Skip to content

Commit

Permalink
Add WindowBuilder::with_active
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored and kchibisov committed Jan 19, 2023
1 parent 809162f commit d26c160
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- On Windows and macOS, add `WindowBuilder::with_active`.
- Add `Window::is_minimized`.
- On X11, fix errors handled during `register_xlib_error_hook` invocation bleeding into winit.
- Add `Window::has_focus`.
Expand Down
3 changes: 3 additions & 0 deletions src/platform_impl/macos/appkit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ extern_methods!(
#[sel(makeKeyAndOrderFront:)]
pub fn makeKeyAndOrderFront(&self, sender: Option<&Object>);

#[sel(orderFront:)]
pub fn orderFront(&self, sender: Option<&Object>);

#[sel(miniaturize:)]
pub fn miniaturize(&self, sender: Option<&Object>);

Expand Down
11 changes: 9 additions & 2 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size, Size::Logical,
},
error::{ExternalError, NotSupportedError, OsError as RootOsError},
event::WindowEvent,
icon::Icon,
platform::macos::WindowExtMacOS,
platform_impl::platform::{
Expand Down Expand Up @@ -464,14 +465,20 @@ impl WinitWindow {
// state, since otherwise we'll briefly see the window at normal size
// before it transitions.
if attrs.visible {
// Tightly linked with `app_state::window_activation_hack`
this.makeKeyAndOrderFront(None);
if attrs.active {
// Tightly linked with `app_state::window_activation_hack`
this.makeKeyAndOrderFront(None);
} else {
this.orderFront(None);
}
}

if attrs.maximized {
this.set_maximized(attrs.maximized);
}

delegate.emit_event(WindowEvent::Focused(false));

Ok((this, delegate))
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl WinitWindowDelegate {
}
}

fn emit_event(&self, event: WindowEvent<'static>) {
pub(crate) fn emit_event(&self, event: WindowEvent<'static>) {
let event = Event::WindowEvent {
window_id: WindowId(self.window.id()),
event,
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,7 @@ where
WindowFlags::NO_BACK_BUFFER,
pl_attribs.no_redirection_bitmap,
);
window_flags.set(WindowFlags::MARKER_NO_ACTIVATE, !attributes.active);
window_flags.set(WindowFlags::TRANSPARENT, attributes.transparent);
// WindowFlags::VISIBLE and MAXIMIZED are set down below after the window has been configured.
window_flags.set(WindowFlags::RESIZABLE, attributes.resizable);
Expand Down
10 changes: 9 additions & 1 deletion src/platform_impl/windows/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ bitflags! {
/// Drop shadow for undecorated windows.
const MARKER_UNDECORATED_SHADOW = 1 << 20;

const MARKER_NO_ACTIVATE = 1 << 18;

const EXCLUSIVE_FULLSCREEN_OR_MASK = WindowFlags::ALWAYS_ON_TOP.bits;
}
}
Expand Down Expand Up @@ -306,8 +308,14 @@ impl WindowFlags {
}

if new.contains(WindowFlags::VISIBLE) {
let flag = if self.contains(WindowFlags::MARKER_NO_ACTIVATE) {
self.set(WindowFlags::MARKER_NO_ACTIVATE, false);
SWP_NOACTIVATE
} else {
SW_SHOW
};
unsafe {
ShowWindow(window, SW_SHOW);
ShowWindow(window, flag);
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub(crate) struct WindowAttributes {
pub content_protected: bool,
pub window_level: WindowLevel,
pub parent_window: Option<RawWindowHandle>,
pub active: bool,
}

impl Default for WindowAttributes {
Expand All @@ -163,6 +164,7 @@ impl Default for WindowAttributes {
resize_increments: None,
content_protected: false,
parent_window: None,
active: true,
}
}
}
Expand Down Expand Up @@ -405,6 +407,17 @@ impl WindowBuilder {
self
}

/// Whether the window will be initially focused or not.
///
/// ## Platform-specific:
///
/// **Android / iOS / X11 / Wayland / Orbital:** Unsupported.
#[inline]
pub fn with_active(mut self, active: bool) -> WindowBuilder {
self.window.active = active;
self
}

/// Build window with parent window.
///
/// The default is `None`.
Expand Down

0 comments on commit d26c160

Please sign in to comment.