Skip to content

Commit 19b83b9

Browse files
committed
fix app open logic
1 parent b1fdfa5 commit 19b83b9

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src-tauri/src/ax/application_tree.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub enum SearchParam {
9090
ByWindowId(WindowId),
9191
BySpaceId(SpaceId),
9292
ByDisplayId(DisplayId),
93+
ByName(String),
9394
Focused,
9495
}
9596

@@ -183,6 +184,11 @@ impl ApplicationTree {
183184
SearchParam::BySpaceId(space_id) => self.search_by_space_id(space_id),
184185
SearchParam::ByDisplayId(display_id) => self.search_by_display_id(display_id),
185186
SearchParam::Focused => self.search_by_focused(),
187+
SearchParam::ByName(name) => self
188+
.flatten()
189+
.into_iter()
190+
.filter(|res| res.app_name == name)
191+
.collect(),
186192
}
187193
}
188194
}

src-tauri/src/ax/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ impl AX {
148148
Some(())
149149
}
150150

151+
pub fn try_focus_app(&mut self, app_name: &str) {
152+
if let Some(res) = self
153+
.application_tree
154+
.search(SearchParam::ByName(app_name.to_string()))
155+
.first()
156+
{
157+
let SearchResult {
158+
pid,
159+
window_id,
160+
space_id,
161+
..
162+
} = res;
163+
let _ = self.focus_space(*space_id);
164+
self.focus.focus(&self.app, *pid, Some(*window_id));
165+
}
166+
}
167+
151168
/// Find window, switch to its Space, then focus it.
152169
pub fn focus_window(&mut self, window_id: WindowId) {
153170
if let Some(res) = self

src-tauri/src/cmd.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::sync::{Arc, RwLock};
1+
use std::{
2+
sync::{Arc, RwLock},
3+
thread,
4+
};
25

36
use lightsky::WindowId;
47
use tauri::Manager;
@@ -19,10 +22,16 @@ pub fn run_cmd(app: tauri::AppHandle, cmd: Command) -> Result<(), String> {
1922
}
2023
}
2124

22-
fn run_app_cmd(app: tauri::AppHandle, AppInfo { path, .. }: AppInfo) -> Result<(), String> {
25+
fn run_app_cmd(app: tauri::AppHandle, AppInfo { path, name }: AppInfo) -> Result<(), String> {
2326
app.opener()
2427
.open_path(path, None::<&str>)
25-
.map_err(|e| e.to_string())
28+
.map_err(|e| e.to_string())?;
29+
thread::sleep(std::time::Duration::from_millis(500));
30+
let ax = app.state::<Arc<RwLock<AX>>>();
31+
let mut ax = ax.write().unwrap();
32+
ax.refresh();
33+
ax.try_focus_app(&name);
34+
Ok(())
2635
}
2736

2837
fn run_switch_cmd(app: tauri::AppHandle, target: WindowTarget) -> Result<(), String> {

0 commit comments

Comments
 (0)