Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions native/taskbar-lyric/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,32 @@ fn create_strategy() -> Option<Box<dyn TaskbarStrategy>> {
let build_num = get_windows_build_number();
debug!("Windows 版本号: {build_num}");

let mut strategy: Box<dyn TaskbarStrategy> = if build_num >= 22000 {
debug!("选择 Win11Strategy");
Box::new(Win11Strategy::new())
} else {
debug!("选择 LegacyStrategy");
Box::new(LegacyStrategy::new())
};

if strategy.init() {
Some(strategy)
} else {
warn!("初始化主要策略时失败,回退到 Win11Strategy");
let mut fallback = Box::new(Win11Strategy::new());
if fallback.init() {
info!("回退策略初始化成功");
Some(fallback)
let (mut primary, mut secondary): (Box<dyn TaskbarStrategy>, Box<dyn TaskbarStrategy>) =
if build_num >= 22000 {
(
Box::new(Win11Strategy::new()),
Box::new(LegacyStrategy::new()),
)
} else {
error!("所有策略均初始化失败");
None
}
(
Box::new(LegacyStrategy::new()),
Box::new(Win11Strategy::new()),
)
};

if primary.init() {
debug!("首选策略初始化成功");
return Some(primary);
}

warn!("首选策略失效,尝试备选策略");
if secondary.init() {
debug!("备选策略初始化成功");
return Some(secondary);
}

error!("未检测到支持的任务栏结构");
None
}

#[napi]
Expand Down
23 changes: 16 additions & 7 deletions native/taskbar-lyric/src/strategy/win11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
LayoutParams, TaskbarStrategy,
strategy::{Rect, TaskbarLayout, Win11Layout},
uia::TaskbarScanner,
utils::{check_registry_value, find_taskbar_hwnd, modify_window_long},
utils::{BRIDGE_CLASS, check_registry_value, find_taskbar_hwnd, modify_window_long},
};

pub struct Win11Strategy {
Expand All @@ -38,13 +38,22 @@ impl Win11Strategy {

impl TaskbarStrategy for Win11Strategy {
fn init(&mut self) -> bool {
if let Some(hwnd) = find_taskbar_hwnd() {
self.h_taskbar = hwnd;
debug!(?hwnd, "找到 Shell_TrayWnd");
return true;
let Some(hwnd) = find_taskbar_hwnd() else {
error!("初始化失败,找不到 Shell_TrayWnd");
return false;
};

let h_bridge =
unsafe { FindWindowExW(Some(hwnd), None, BRIDGE_CLASS, None) }.unwrap_or_default();

if h_bridge.0.is_null() {
error!("初始化失败,找不到 XAML 桥");
return false;
}
error!("初始化失败,找不到 Shell_TrayWnd");
false

self.h_taskbar = hwnd;
debug!(?hwnd, "Win11 策略初始化成功");
true
}

fn embed_window(&self, child_wnd: HWND) -> bool {
Expand Down
26 changes: 11 additions & 15 deletions native/taskbar-lyric/src/uia.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
use anyhow::{Context, Result, bail};
use windows::{
Win32::{
Foundation::{HWND, RPC_E_CHANGED_MODE},
System::Com::{
CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx,
CoUninitialize,
},
UI::{
Accessibility::{
CUIAutomation, IUIAutomation, IUIAutomationElement, TreeScope_Descendants,
},
WindowsAndMessaging::FindWindowExW,
use windows::Win32::{
Foundation::{HWND, RPC_E_CHANGED_MODE},
System::Com::{
CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx,
CoUninitialize,
},
UI::{
Accessibility::{
CUIAutomation, IUIAutomation, IUIAutomationElement, TreeScope_Descendants,
},
WindowsAndMessaging::FindWindowExW,
},
core::{PCWSTR, w},
};

use crate::strategy::Rect;
use crate::{strategy::Rect, utils::BRIDGE_CLASS};

const BRIDGE_CLASS: PCWSTR = w!("Windows.UI.Composition.DesktopWindowContentBridge");
const CLASS_TASKLIST_BUTTON: &str = "Taskbar.TaskListButtonAutomationPeer";
const ID_START_BUTTON: &str = "StartButton";
const ID_SEARCH_BUTTON: &str = "SearchButton";
Expand Down
3 changes: 3 additions & 0 deletions native/taskbar-lyric/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ use windows::{
},
core::w,
};
use windows_core::PCWSTR;
use winreg::{
RegKey,
enums::{HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE},
};

pub const BRIDGE_CLASS: PCWSTR = w!("Windows.UI.Composition.DesktopWindowContentBridge");

pub const REG_KEY_ADVANCED: &str =
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced";

Expand Down