fix(desktop): lock appbar to edge
This commit is contained in:
@@ -1,49 +1,9 @@
|
||||
use tauri::Window;
|
||||
|
||||
#[cfg(windows)]
|
||||
use tauri::Manager;
|
||||
#[cfg(not(windows))]
|
||||
use tauri::{PhysicalPosition, PhysicalSize};
|
||||
|
||||
#[cfg(windows)]
|
||||
mod windows_appbar;
|
||||
|
||||
#[tauri::command]
|
||||
fn dock_window(window: Window, side: String) -> Result<(), String> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let hwnd = window.hwnd().map_err(|error| error.to_string())?;
|
||||
return windows_appbar::set_edge(hwnd, side == "left").map_err(|error| error.to_string());
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
let monitor = window
|
||||
.current_monitor()
|
||||
.map_err(|error| error.to_string())?
|
||||
.ok_or_else(|| "Unable to identify the current monitor".to_string())?;
|
||||
let window_size = window.outer_size().map_err(|error| error.to_string())?;
|
||||
let work_area = monitor.work_area();
|
||||
let dock_size = PhysicalSize::new(window_size.width, work_area.size.height);
|
||||
let x = if side == "left" {
|
||||
work_area.position.x
|
||||
} else {
|
||||
work_area.position.x + work_area.size.width as i32 - dock_size.width as i32
|
||||
};
|
||||
|
||||
window
|
||||
.set_size(dock_size)
|
||||
.map_err(|error| error.to_string())?;
|
||||
window
|
||||
.set_position(PhysicalPosition::new(x, work_area.position.y))
|
||||
.map_err(|error| error.to_string())?;
|
||||
window
|
||||
.set_always_on_top(true)
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
@@ -58,7 +18,6 @@ pub fn run() {
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![dock_window])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("failed to build SenlinAI App")
|
||||
.run(|_, event| {
|
||||
|
||||
@@ -12,7 +12,7 @@ use windows::{
|
||||
UI::{
|
||||
Shell::{
|
||||
DefSubclassProc, RemoveWindowSubclass, SHAppBarMessage, SetWindowSubclass,
|
||||
ABE_LEFT, ABE_RIGHT, ABM_ACTIVATE, ABM_NEW, ABM_QUERYPOS, ABM_REMOVE, ABM_SETPOS,
|
||||
ABE_RIGHT, ABM_ACTIVATE, ABM_NEW, ABM_QUERYPOS, ABM_REMOVE, ABM_SETPOS,
|
||||
ABM_WINDOWPOSCHANGED, ABN_POSCHANGED, APPBARDATA,
|
||||
},
|
||||
WindowsAndMessaging::{
|
||||
@@ -27,7 +27,6 @@ const SUBCLASS_ID: usize = 0x5341_4142; // "SAAB" (SenlinAI AppBar)
|
||||
|
||||
static CALLBACK_MESSAGE: AtomicU32 = AtomicU32::new(0);
|
||||
static REGISTERED_HWND: AtomicIsize = AtomicIsize::new(0);
|
||||
static EDGE: AtomicU32 = AtomicU32::new(ABE_RIGHT);
|
||||
static LAYOUT_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Registers the main App window as a right-edge AppBar.
|
||||
@@ -65,12 +64,6 @@ pub fn register(hwnd: HWND) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Repositions the AppBar after the App UI selects the left or right edge.
|
||||
pub fn set_edge(hwnd: HWND, left: bool) -> Result<()> {
|
||||
EDGE.store(if left { ABE_LEFT } else { ABE_RIGHT }, Ordering::Release);
|
||||
layout(hwnd)
|
||||
}
|
||||
|
||||
/// Removes the AppBar reservation before the Tauri window is destroyed.
|
||||
pub fn unregister() {
|
||||
let raw_hwnd = REGISTERED_HWND.swap(0, Ordering::AcqRel);
|
||||
@@ -137,23 +130,14 @@ fn layout_inner(hwnd: HWND) -> Result<()> {
|
||||
};
|
||||
unsafe { GetMonitorInfoW(monitor, &mut monitor_info).ok()? };
|
||||
|
||||
let edge = EDGE.load(Ordering::Acquire);
|
||||
let mut data = appbar_data(hwnd);
|
||||
data.uEdge = edge;
|
||||
data.uEdge = ABE_RIGHT;
|
||||
data.rc = monitor_info.rcMonitor;
|
||||
if edge == ABE_LEFT {
|
||||
data.rc.right = data.rc.left + width;
|
||||
} else {
|
||||
data.rc.left = data.rc.right - width;
|
||||
}
|
||||
data.rc.left = data.rc.right - width;
|
||||
|
||||
unsafe {
|
||||
SHAppBarMessage(ABM_QUERYPOS, &mut data);
|
||||
if edge == ABE_LEFT {
|
||||
data.rc.right = data.rc.left + width;
|
||||
} else {
|
||||
data.rc.left = data.rc.right - width;
|
||||
}
|
||||
data.rc.left = data.rc.right - width;
|
||||
SHAppBarMessage(ABM_SETPOS, &mut data);
|
||||
SetWindowPos(
|
||||
hwnd,
|
||||
|
||||
Reference in New Issue
Block a user