mirror of
https://github.com/Stremio/stremio-shell-ng.git
synced 2026-04-21 16:01:56 +00:00
Center window as helper function
This commit is contained in:
parent
ae5f986737
commit
81dbe73be0
2 changed files with 33 additions and 28 deletions
|
|
@ -2,7 +2,6 @@ use native_windows_derive::NwgUi;
|
||||||
use native_windows_gui as nwg;
|
use native_windows_gui as nwg;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::cmp;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use winapi::um::winuser::WS_EX_TOPMOST;
|
use winapi::um::winuser::WS_EX_TOPMOST;
|
||||||
|
|
@ -77,17 +76,10 @@ impl MainWindow {
|
||||||
}
|
}
|
||||||
fn on_init(&self) {
|
fn on_init(&self) {
|
||||||
self.webview.endpoint.set(self.webui_url.clone()).ok();
|
self.webview.endpoint.set(self.webui_url.clone()).ok();
|
||||||
let small_side = cmp::min(nwg::Monitor::width(), nwg::Monitor::height()) * 70 / 100;
|
if let Some(hwnd) = self.window.handle.hwnd() {
|
||||||
let dimensions = (
|
let mut saved_style = self.saved_window_style.borrow_mut();
|
||||||
cmp::max(small_side * 16 / 9, Self::MIN_WIDTH),
|
saved_style.center_window(hwnd, Self::MIN_WIDTH, Self::MIN_HEIGHT);
|
||||||
cmp::max(small_side, Self::MIN_HEIGHT),
|
}
|
||||||
);
|
|
||||||
let [total_width, total_height] = [nwg::Monitor::width(), nwg::Monitor::height()];
|
|
||||||
let x = (total_width - dimensions.0) / 2;
|
|
||||||
let y = (total_height - dimensions.1) / 2;
|
|
||||||
self.window
|
|
||||||
.set_size(dimensions.0 as u32, dimensions.1 as u32);
|
|
||||||
self.window.set_position(x, y);
|
|
||||||
|
|
||||||
self.tray.tray_show_hide.set_checked(true);
|
self.tray.tray_show_hide.set_checked(true);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::{cmp, mem};
|
||||||
use winapi::shared::windef::HWND__;
|
use winapi::shared::windef::HWND__;
|
||||||
use winapi::um::winuser::{
|
use winapi::um::winuser::{
|
||||||
GetForegroundWindow, GetSystemMetrics, GetWindowLongA, GetWindowRect, IsIconic, IsZoomed,
|
GetForegroundWindow, GetSystemMetrics, GetWindowLongA, GetWindowRect, IsIconic, IsZoomed,
|
||||||
|
|
@ -6,7 +7,6 @@ use winapi::um::winuser::{
|
||||||
WS_EX_CLIENTEDGE, WS_EX_DLGMODALFRAME, WS_EX_STATICEDGE, WS_EX_TOPMOST, WS_EX_WINDOWEDGE,
|
WS_EX_CLIENTEDGE, WS_EX_DLGMODALFRAME, WS_EX_STATICEDGE, WS_EX_TOPMOST, WS_EX_WINDOWEDGE,
|
||||||
WS_THICKFRAME,
|
WS_THICKFRAME,
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://doc.qt.io/qt-5/qt.html#WindowState-enum
|
// https://doc.qt.io/qt-5/qt.html#WindowState-enum
|
||||||
bitflags! {
|
bitflags! {
|
||||||
struct WindowState: u8 {
|
struct WindowState: u8 {
|
||||||
|
|
@ -21,7 +21,7 @@ bitflags! {
|
||||||
pub struct WindowStyle {
|
pub struct WindowStyle {
|
||||||
pub full_screen: bool,
|
pub full_screen: bool,
|
||||||
pub pos: (i32, i32),
|
pub pos: (i32, i32),
|
||||||
pub size: (u32, u32),
|
pub size: (i32, i32),
|
||||||
pub style: i32,
|
pub style: i32,
|
||||||
pub ex_style: i32,
|
pub ex_style: i32,
|
||||||
}
|
}
|
||||||
|
|
@ -43,6 +43,30 @@ impl WindowStyle {
|
||||||
}
|
}
|
||||||
state.bits() as u32
|
state.bits() as u32
|
||||||
}
|
}
|
||||||
|
pub fn show_window_at(&self, hwnd: *mut HWND__, pos: *mut HWND__) {
|
||||||
|
unsafe {
|
||||||
|
SetWindowPos(
|
||||||
|
hwnd,
|
||||||
|
pos,
|
||||||
|
self.pos.0,
|
||||||
|
self.pos.1,
|
||||||
|
self.size.0,
|
||||||
|
self.size.1,
|
||||||
|
SWP_FRAMECHANGED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn center_window(&mut self, hwnd: *mut HWND__, min_width: i32, min_height: i32) {
|
||||||
|
let monitor_w = unsafe { GetSystemMetrics(SM_CXSCREEN) };
|
||||||
|
let monitor_h = unsafe { GetSystemMetrics(SM_CYSCREEN) };
|
||||||
|
let small_side = cmp::min(monitor_w, monitor_h) * 70 / 100;
|
||||||
|
self.size = (
|
||||||
|
cmp::max(small_side * 16 / 9, min_width),
|
||||||
|
cmp::max(small_side, min_height),
|
||||||
|
);
|
||||||
|
self.pos = ((monitor_w - self.size.0) / 2, (monitor_h - self.size.1) / 2);
|
||||||
|
self.show_window_at(hwnd, HWND_NOTOPMOST);
|
||||||
|
}
|
||||||
pub fn toggle_full_screen(&mut self, hwnd: *mut HWND__) {
|
pub fn toggle_full_screen(&mut self, hwnd: *mut HWND__) {
|
||||||
if self.full_screen {
|
if self.full_screen {
|
||||||
let topmost = if self.ex_style as u32 & WS_EX_TOPMOST == WS_EX_TOPMOST {
|
let topmost = if self.ex_style as u32 & WS_EX_TOPMOST == WS_EX_TOPMOST {
|
||||||
|
|
@ -53,26 +77,15 @@ impl WindowStyle {
|
||||||
unsafe {
|
unsafe {
|
||||||
SetWindowLongA(hwnd, GWL_STYLE, self.style);
|
SetWindowLongA(hwnd, GWL_STYLE, self.style);
|
||||||
SetWindowLongA(hwnd, GWL_EXSTYLE, self.ex_style);
|
SetWindowLongA(hwnd, GWL_EXSTYLE, self.ex_style);
|
||||||
SetWindowPos(
|
|
||||||
hwnd,
|
|
||||||
topmost,
|
|
||||||
self.pos.0,
|
|
||||||
self.pos.1,
|
|
||||||
self.size.0 as i32,
|
|
||||||
self.size.1 as i32,
|
|
||||||
SWP_FRAMECHANGED,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
self.show_window_at(hwnd, topmost);
|
||||||
self.full_screen = false;
|
self.full_screen = false;
|
||||||
} else {
|
} else {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut rect = std::mem::zeroed();
|
let mut rect = mem::zeroed();
|
||||||
GetWindowRect(hwnd, &mut rect);
|
GetWindowRect(hwnd, &mut rect);
|
||||||
self.pos = (rect.left, rect.top);
|
self.pos = (rect.left, rect.top);
|
||||||
self.size = (
|
self.size = ((rect.right - rect.left), (rect.bottom - rect.top));
|
||||||
(rect.right - rect.left) as u32,
|
|
||||||
(rect.bottom - rect.top) as u32,
|
|
||||||
);
|
|
||||||
self.style = GetWindowLongA(hwnd, GWL_STYLE);
|
self.style = GetWindowLongA(hwnd, GWL_STYLE);
|
||||||
self.ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
|
self.ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
|
||||||
SetWindowLongA(
|
SetWindowLongA(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue