From 46d3039364adc3fcc6064262e4bdad85bc8e7e2d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 12 Nov 2023 12:20:06 +0100 Subject: [PATCH] Clean up ViewportCommands --- crates/egui-winit/src/lib.rs | 53 +++++++++++++------------- crates/egui/src/viewport.rs | 74 ++++++++++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 436c8f58e76..c82b0beb1fa 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -997,7 +997,7 @@ pub fn process_viewport_commands( for command in commands { match command { - egui::ViewportCommand::Drag => { + egui::ViewportCommand::StartDrag => { // if this is not checked on x11 the input will be permanently taken until the app is killed! if is_viewport_focused { // TODO possible return the error to `egui::Context` @@ -1068,10 +1068,10 @@ pub fn process_viewport_commands( window.set_fullscreen(v.then_some(winit::window::Fullscreen::Borderless(None))); } ViewportCommand::Decorations(v) => window.set_decorations(v), - ViewportCommand::WindowLevel(o) => window.set_window_level(match o { - 1 => WindowLevel::AlwaysOnBottom, - 2 => WindowLevel::AlwaysOnTop, - _ => WindowLevel::Normal, + ViewportCommand::WindowLevel(l) => window.set_window_level(match l { + egui::viewport::WindowLevel::AlwaysOnBottom => WindowLevel::AlwaysOnBottom, + egui::viewport::WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnTop, + egui::viewport::WindowLevel::Normal => WindowLevel::Normal, }), ViewportCommand::WindowIcon(icon) => { window.set_window_icon(icon.map(|icon| { @@ -1087,25 +1087,26 @@ pub fn process_viewport_commands( window.set_ime_position(LogicalPosition::new(pos.x, pos.y)); } ViewportCommand::IMEAllowed(v) => window.set_ime_allowed(v), - ViewportCommand::IMEPurpose(o) => window.set_ime_purpose(match o { - 1 => winit::window::ImePurpose::Password, - 2 => winit::window::ImePurpose::Terminal, - _ => winit::window::ImePurpose::Normal, + ViewportCommand::IMEPurpose(p) => window.set_ime_purpose(match p { + egui::viewport::IMEPurpose::Password => winit::window::ImePurpose::Password, + egui::viewport::IMEPurpose::Terminal => winit::window::ImePurpose::Terminal, + egui::viewport::IMEPurpose::Normal => winit::window::ImePurpose::Normal, + }), + ViewportCommand::RequestUserAttention(a) => { + window.request_user_attention(a.map(|a| match a { + egui::viewport::UserAttentionType::Critical => { + winit::window::UserAttentionType::Critical + } + egui::viewport::UserAttentionType::Informational => { + winit::window::UserAttentionType::Informational + } + })); + } + ViewportCommand::SetTheme(t) => window.set_theme(match t { + egui::SystemTheme::Light => Some(winit::window::Theme::Light), + egui::SystemTheme::Dark => Some(winit::window::Theme::Dark), + egui::SystemTheme::SystemDefault => None, }), - ViewportCommand::RequestUserAttention(o) => window.request_user_attention(o.map(|o| { - if o == 1 { - winit::window::UserAttentionType::Critical - } else { - winit::window::UserAttentionType::Informational - } - })), - ViewportCommand::SetTheme(o) => window.set_theme(o.map(|o| { - if o == 1 { - winit::window::Theme::Dark - } else { - winit::window::Theme::Light - } - })), ViewportCommand::ContentProtected(v) => window.set_content_protected(v), ViewportCommand::CursorPosition(pos) => { if let Err(err) = window.set_cursor_position(LogicalPosition::new(pos.x, pos.y)) { @@ -1114,9 +1115,9 @@ pub fn process_viewport_commands( } ViewportCommand::CursorGrab(o) => { if let Err(err) = window.set_cursor_grab(match o { - 1 => CursorGrabMode::Confined, - 2 => CursorGrabMode::Locked, - _ => CursorGrabMode::None, + egui::viewport::CursorGrab::None => CursorGrabMode::None, + egui::viewport::CursorGrab::Confined => CursorGrabMode::Confined, + egui::viewport::CursorGrab::Locked => CursorGrabMode::Locked, }) { log::error!("{err}"); } diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index 88ff258ea23..5065f7cdf3d 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -573,16 +573,65 @@ impl ViewportBuilder { } } +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub enum WindowLevel { + Normal, + AlwaysOnBottom, + AlwaysOnTop, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub enum IMEPurpose { + Normal, + Password, + Terminal, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub enum SystemTheme { + Light, + Dark, + SystemDefault, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub enum CursorGrab { + None, + Confined, + Locked, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub enum UserAttentionType { + Informational, + Critical, +} + /// You can send a [`ViewportCommand`] to the viewport with [`Context::viewport_command`]. /// /// All coordinates are in logical points. #[derive(Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum ViewportCommand { + /// Set the title Title(String), + + /// Turn the window transparent or not. Transparent(bool), + + /// Set the visibility of the window. Visible(bool), - Drag, + + /// Moves the window with the left mouse button until the button is released. + /// + /// There's no guarantee that this will work unless the left mouse button was pressed + /// immediately before this function is called. + StartDrag, /// Set the outer position of the viewport, i.e. moves the window. OuterPosition(Pos2), @@ -610,7 +659,10 @@ pub enum ViewportCommand { left: bool, }, + /// Can the window be resized? Resizable(bool), + + /// Set which window buttons are enabled EnableButtons { close: bool, minimized: bool, @@ -619,30 +671,28 @@ pub enum ViewportCommand { Minimized(bool), Maximized(bool), Fullscreen(bool), + + /// Show window decorations, i.e. the chrome around the content + /// with the title bar, close buttons, resize handles, etc. Decorations(bool), - /// 0 = Normal, 1 = AlwaysOnBottom, 2 = AlwaysOnTop - WindowLevel(u8), + WindowLevel(WindowLevel), WindowIcon(Option), + IMEPosition(Pos2), IMEAllowed(bool), + IMEPurpose(IMEPurpose), - /// 0 = Normal, 1 = Password, 2 = Terminal - IMEPurpose(u8), - - /// 0 = Informational, 1 = Critical - RequestUserAttention(Option), + RequestUserAttention(Option), - /// 0 = Light, 1 = Dark, `None` = system default. - SetTheme(Option), + SetTheme(SystemTheme), ContentProtected(bool), /// Will probably not work as expected! CursorPosition(Pos2), - /// 0 = None, 1 = Confined, 2 = Locked - CursorGrab(u8), + CursorGrab(CursorGrab), CursorVisible(bool),