Skip to content

Commit

Permalink
Clean up ViewportCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 12, 2023
1 parent 2f53f7d commit 46d3039
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 38 deletions.
53 changes: 27 additions & 26 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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| {
Expand All @@ -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)) {
Expand All @@ -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}");
}
Expand Down
74 changes: 62 additions & 12 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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<ColorImage>),

IMEPosition(Pos2),
IMEAllowed(bool),
IMEPurpose(IMEPurpose),

/// 0 = Normal, 1 = Password, 2 = Terminal
IMEPurpose(u8),

/// 0 = Informational, 1 = Critical
RequestUserAttention(Option<u8>),
RequestUserAttention(Option<UserAttentionType>),

/// 0 = Light, 1 = Dark, `None` = system default.
SetTheme(Option<u8>),
SetTheme(SystemTheme),

ContentProtected(bool),

/// Will probably not work as expected!
CursorPosition(Pos2),

/// 0 = None, 1 = Confined, 2 = Locked
CursorGrab(u8),
CursorGrab(CursorGrab),

CursorVisible(bool),

Expand Down

0 comments on commit 46d3039

Please sign in to comment.