Skip to content

Commit

Permalink
window: handle resizable
Browse files Browse the repository at this point in the history
  • Loading branch information
kchibisov committed Mar 8, 2023
1 parent 2edfca4 commit 45ceaaf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/seat/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl KeyboardHandler for WinitState {
) {
let window_id = wayland::make_wid(surface);

// Mark the window as not focused.
// XXX The check whether the window exists is essential as we might get a nil surface...
match self.windows.get_mut().get(&window_id) {
Some(window) => window.lock().unwrap().set_has_focus(false),
None => return,
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/linux/wayland/seat/text_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl Dispatch<ZwpTextInputV3, TextInputData, WinitState> for TextInputState {

let window_id = wayland::make_wid(&surface);

// XXX this check is essential, because `leave` could have a
// refence to nil surface...
let mut window = match windows.get(&window_id) {
Some(window) => window.lock().unwrap(),
None => return,
Expand Down
43 changes: 12 additions & 31 deletions src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ pub struct Window {
/// Handle to the main queue to perform requests.
queue_handle: QueueHandle<WinitState>,

/// Whether the window is resizeable.
resizeable: AtomicBool,

/// Window requests to the event loop.
window_requests: Arc<WindowRequests>,

Expand Down Expand Up @@ -123,29 +120,22 @@ impl Window {
attributes.preferred_theme,
);

// Set the app_id.
if let Some(name) = platform_attributes.name.map(|name| name.general) {
window.set_app_id(name);
}

// Set the window title.
window_state.set_title(attributes.title);

// Set the min and max sizes.
let min_size = attributes.min_inner_size.map(|size| size.to_logical(1.));
let max_size = attributes.max_inner_size.map(|size| size.to_logical(1.));
window_state.set_min_inner_size(min_size);
window_state.set_max_inner_size(max_size);

// Set the min and max sizes.
{
let (min_size, max_size) = if attributes.resizable {
(min_size.map(Into::into), max_size.map(Into::into))
} else {
// Non-resizable implies that the min and max sizes are set to the same value.
(Some(size), Some(size))
};

window_state.set_min_inner_size(min_size);
window_state.set_max_inner_size(max_size);
}

// Set the app_id.
if let Some(name) = platform_attributes.name.map(|name| name.general) {
window.set_app_id(name);
}
// Non-resizable implies that the min and max sizes are set to the same value.
window_state.set_resizable(attributes.resizable);

// Set startup mode.
match attributes.fullscreen.map(Into::into) {
Expand Down Expand Up @@ -221,7 +211,6 @@ impl Window {
window_state,
queue_handle,
xdg_activation,
resizeable: AtomicBool::new(attributes.resizable),
attention_requested: Arc::new(AtomicBool::new(false)),
event_loop_awakener,
window_requests,
Expand Down Expand Up @@ -360,20 +349,12 @@ impl Window {

#[inline]
pub fn set_resizable(&self, resizable: bool) {
self.resizeable.store(resizable, Ordering::Relaxed);
if resizable {
// Restore min/max sizes of the window.
self.window_state.lock().unwrap().reload_min_max_hints();
} else {
let size = self.window_state.lock().unwrap().inner_size().into();
self.set_min_inner_size(Some(size));
self.set_max_inner_size(Some(size));
}
self.window_state.lock().unwrap().set_resizable(resizable);
}

#[inline]
pub fn is_resizable(&self) -> bool {
self.resizeable.load(Ordering::Relaxed)
self.window_state.lock().unwrap().resizable()
}

#[inline]
Expand Down
32 changes: 32 additions & 0 deletions src/platform_impl/linux/wayland/window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub struct WindowState {
/// The current window title.
title: String,

/// Whether the frame is resizable.
resizable: bool,

/// Whether the window has focus.
has_focus: bool,

Expand Down Expand Up @@ -293,6 +296,34 @@ impl WindowState {
}
}

/// Get the stored resizable state.
#[inline]
pub fn resizable(&self) -> bool {
self.resizable
}

/// Set the resizable state on the window.
#[inline]
pub fn set_resizable(&mut self, resizable: bool) {
if self.resizable == resizable {
return;
}

self.resizable = resizable;
if resizable {
// Restore min/max sizes of the window.
self.reload_min_max_hints();
} else {
self.set_min_inner_size(Some(self.size));
self.set_max_inner_size(Some(self.size));
}

// Reload the state on the frame as well.
if let Some(frame) = self.frame.as_mut() {
frame.set_resizable(resizable);
}
}

/// Whether the window is focused.
#[inline]
pub fn has_focus(&self) -> bool {
Expand Down Expand Up @@ -378,6 +409,7 @@ impl WindowState {
text_inputs: Vec::new(),
title: String::default(),
transparent: false,
resizable: true,
viewport,
window: ManuallyDrop::new(window),
}
Expand Down

0 comments on commit 45ceaaf

Please sign in to comment.