1
0
Fork 0

add window resize hints

This commit is contained in:
Billy Messenger 2020-10-15 16:31:38 -05:00
parent 0758e4834b
commit aee594d23c
4 changed files with 53 additions and 10 deletions

View file

@ -25,8 +25,8 @@ impl WindowHandler for MyProgram {
fn main() {
let window_open_options = baseview::WindowOpenOptions {
title: "baseview".into(),
logical_width: 512,
logical_height: 512,
logical_size: (512, 512),
resize: baseview::WindowResize::None,
scale: 1.0,
parent: baseview::Parent::None,
};

View file

@ -32,13 +32,22 @@ pub enum Parent {
unsafe impl Send for Parent {}
pub enum WindowResize {
None,
MinMax {
min_logical_size: (u32, u32),
max_logical_size: (u32, u32),
keep_aspect: bool,
},
}
pub struct WindowOpenOptions {
pub title: String,
/// The logical width of the window
pub logical_width: u32,
/// The logical height of the window
pub logical_height: u32,
/// The logical width and height of the window
pub logical_size: (u32, u32),
pub resize: WindowResize,
/// The dpi scale factor. This will used in conjunction with the dpi scale
/// factor of the system.

View file

@ -12,7 +12,7 @@ use raw_window_handle::{
use super::XcbConnection;
use crate::{
Event, KeyboardEvent, MouseButton, MouseCursor, MouseEvent, Parent, ScrollDelta, WindowEvent,
WindowHandle, WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult,
WindowHandle, WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult, WindowResize,
};
pub struct Window {
@ -81,9 +81,10 @@ impl Window {
let scaling = xcb_connection.get_scaling().unwrap_or(1.0) * options.scale;
let (logical_width, logical_height) = options.logical_size;
let window_info = WindowInfo::from_logical_size(
options.logical_width,
options.logical_height,
logical_width,
logical_height,
scaling
);
@ -126,6 +127,37 @@ impl Window {
title.as_bytes(),
);
match options.resize {
WindowResize::MinMax { min_logical_size, max_logical_size, keep_aspect } => {
let size_hints = if keep_aspect {
xcb_util::icccm::SizeHints::empty()
.min_size(min_logical_size.0 as i32, min_logical_size.1 as i32)
.max_size(max_logical_size.0 as i32, max_logical_size.1 as i32)
.aspect(
(min_logical_size.0 as i32, min_logical_size.1 as i32),
(max_logical_size.0 as i32, max_logical_size.1 as i32),
)
.build()
} else {
xcb_util::icccm::SizeHints::empty()
.min_size(min_logical_size.0 as i32, min_logical_size.1 as i32)
.max_size(max_logical_size.0 as i32, max_logical_size.1 as i32)
.build()
};
xcb_connection.atoms.wm_normal_hints
.map(|wm_normal_hints| {
xcb_util::icccm::set_wm_size_hints(
&xcb_connection.conn,
window_id,
wm_normal_hints,
&size_hints,
);
});
}
_ => {}
}
xcb_connection.atoms.wm_protocols
.zip(xcb_connection.atoms.wm_delete_window)
.map(|(wm_protocols, wm_delete_window)| {

View file

@ -13,6 +13,7 @@ use super::cursor;
pub(crate) struct Atoms {
pub wm_protocols: Option<u32>,
pub wm_delete_window: Option<u32>,
pub wm_normal_hints: Option<u32>,
}
pub struct XcbConnection {
@ -45,7 +46,7 @@ impl XcbConnection {
pub fn new() -> Result<Self, xcb::base::ConnError> {
let (conn, xlib_display) = xcb::Connection::connect_with_xlib_display()?;
let (wm_protocols, wm_delete_window) = intern_atoms!(&conn, WM_PROTOCOLS, WM_DELETE_WINDOW);
let (wm_protocols, wm_delete_window, wm_normal_hints) = intern_atoms!(&conn, WM_PROTOCOLS, WM_DELETE_WINDOW, WM_NORMAL_HINTS);
Ok(Self {
conn,
@ -54,6 +55,7 @@ impl XcbConnection {
atoms: Atoms {
wm_protocols,
wm_delete_window,
wm_normal_hints,
},
cursor_cache: HashMap::new()