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 73d03a9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/platform_impl/linux/wayland/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl MonitorHandle {
let dimensions = output_data.with_output_info(|info| {
info.modes
.iter()
.find_map(|mode| mode.current.then(|| mode.dimensions))
.find_map(|mode| mode.current.then_some(mode.dimensions))
});

match dimensions {
Expand All @@ -79,7 +79,7 @@ impl MonitorHandle {
output_data.with_output_info(|info| {
info.modes
.iter()
.find_map(|mode| mode.current.then(|| mode.refresh_rate as u32))
.find_map(|mode| mode.current.then_some(mode.refresh_rate as u32))
})
}

Expand Down
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: 1 addition & 1 deletion src/platform_impl/linux/wayland/seat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl SeatHandler for WinitState {
if let Some(text_input_state) = seat_state
.text_input
.is_none()
.then(|| self.text_input_state.as_ref())
.then_some(self.text_input_state.as_ref())
.flatten()
{
seat_state.text_input = Some(Arc::new(text_input_state.get_text_input(
Expand Down
6 changes: 4 additions & 2 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 All @@ -111,10 +113,10 @@ impl Dispatch<ZwpTextInputV3, TextInputData, WinitState> for TextInputState {
let text = text.unwrap_or_default();
let cursor_begin = usize::try_from(cursor_begin)
.ok()
.and_then(|idx| text.is_char_boundary(idx).then(|| idx));
.and_then(|idx| text.is_char_boundary(idx).then_some(idx));
let cursor_end = usize::try_from(cursor_end)
.ok()
.and_then(|idx| text.is_char_boundary(idx).then(|| idx));
.and_then(|idx| text.is_char_boundary(idx).then_some(idx));

text_input_data.pending_preedit = Some(Preedit {
text,
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
34 changes: 33 additions & 1 deletion 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 @@ -324,7 +355,7 @@ impl WindowState {
.as_ref()
.map(|configure| configure.decoration_mode == DecorationMode::Client)
.unwrap_or(false);
if let Some(frame) = csd.then(|| self.frame.as_ref()).flatten() {
if let Some(frame) = csd.then_some(self.frame.as_ref()).flatten() {
frame.is_hidden()
} else {
// Server side decorations.
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 73d03a9

Please sign in to comment.