Skip to content

Commit

Permalink
Introduce window::Level enum
Browse files Browse the repository at this point in the history
... and add `level` field to `window::Settings`
  • Loading branch information
hecrj committed May 25, 2023
1 parent b924e86 commit a7fa7e4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
2 changes: 2 additions & 0 deletions core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
pub mod icon;

mod event;
mod level;
mod mode;
mod redraw_request;
mod user_attention;

pub use event::Event;
pub use icon::Icon;
pub use level::Level;
pub use mode::Mode;
pub use redraw_request::RedrawRequest;
pub use user_attention::UserAttention;
19 changes: 19 additions & 0 deletions core/src/window/level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// A window level groups windows with respect to their z-position.
///
/// The relative ordering between windows in different window levels is fixed.
/// The z-order of a window within the same window level may change dynamically
/// on user interaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Level {
/// The default behavior.
#[default]
Normal,

/// The window will always be below normal windows.
///
/// This is useful for a widget-based app.
AlwaysOnBottom,

/// The window will always be on top of normal windows.
AlwaysOnTop,
}
10 changes: 5 additions & 5 deletions src/window/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::window::{Icon, Position};
use crate::window::{Icon, Level, Position};

pub use iced_winit::settings::PlatformSpecific;

Expand Down Expand Up @@ -29,8 +29,8 @@ pub struct Settings {
/// Whether the window should be transparent.
pub transparent: bool,

/// Whether the window will always be on top of other windows.
pub always_on_top: bool,
/// The window [`Level`].
pub level: Level,

/// The icon of the window.
pub icon: Option<Icon>,
Expand All @@ -50,7 +50,7 @@ impl Default for Settings {
resizable: true,
decorations: true,
transparent: false,
always_on_top: false,
level: Level::default(),
icon: None,
platform_specific: Default::default(),
}
Expand All @@ -68,7 +68,7 @@ impl From<Settings> for iced_winit::settings::Window {
resizable: settings.resizable,
decorations: settings.decorations,
transparent: settings.transparent,
always_on_top: settings.always_on_top,
level: settings.level,
icon: settings.icon.map(Icon::into),
platform_specific: settings.platform_specific,
}
Expand Down
13 changes: 13 additions & 0 deletions winit/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ pub fn window_event(
}
}

/// Converts a [`window::Level`] to a [`winit`] window level.
///
/// [`winit`]: https://github.com/rust-windowing/winit
pub fn window_level(level: window::Level) -> winit::window::WindowLevel {
match level {
window::Level::Normal => winit::window::WindowLevel::Normal,
window::Level::AlwaysOnBottom => {
winit::window::WindowLevel::AlwaysOnBottom
}
window::Level::AlwaysOnTop => winit::window::WindowLevel::AlwaysOnTop,
}
}

/// Converts a [`Position`] to a [`winit`] logical position for a given monitor.
///
/// [`winit`]: https://github.com/rust-windowing/winit
Expand Down
18 changes: 7 additions & 11 deletions winit/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ mod platform;
pub use platform::PlatformSpecific;

use crate::conversion;
use crate::core::window::Icon;
use crate::core::window::{Icon, Level};
use crate::Position;

use winit::monitor::MonitorHandle;
use winit::window::{WindowBuilder, WindowLevel};
use winit::window::WindowBuilder;

use std::fmt;

Expand Down Expand Up @@ -81,8 +81,8 @@ pub struct Window {
/// Whether the window should be transparent.
pub transparent: bool,

/// Whether the window will always be on top of other windows.
pub always_on_top: bool,
/// The window [`Level`].
pub level: Level,

/// The window icon, which is also usually used in the taskbar
pub icon: Option<Icon>,
Expand All @@ -102,7 +102,7 @@ impl fmt::Debug for Window {
.field("resizable", &self.resizable)
.field("decorations", &self.decorations)
.field("transparent", &self.transparent)
.field("always_on_top", &self.always_on_top)
.field("level", &self.level)
.field("icon", &self.icon.is_some())
.field("platform_specific", &self.platform_specific)
.finish()
Expand All @@ -121,18 +121,14 @@ impl Window {

let (width, height) = self.size;

let window_level = match self.always_on_top {
true => WindowLevel::AlwaysOnTop,
false => WindowLevel::Normal,
};
window_builder = window_builder
.with_title(title)
.with_inner_size(winit::dpi::LogicalSize { width, height })
.with_resizable(self.resizable)
.with_decorations(self.decorations)
.with_transparent(self.transparent)
.with_window_icon(self.icon.and_then(conversion::icon))
.with_window_level(window_level)
.with_window_level(conversion::window_level(self.level))
.with_visible(self.visible);

if let Some(position) = conversion::position(
Expand Down Expand Up @@ -211,7 +207,7 @@ impl Default for Window {
resizable: true,
decorations: true,
transparent: false,
always_on_top: false,
level: Level::default(),
icon: None,
platform_specific: Default::default(),
}
Expand Down

0 comments on commit a7fa7e4

Please sign in to comment.