Skip to content

Commit cebdfb5

Browse files
authored
Merge pull request #2 from lighthx/feat/mutlti-window
feat: use multi windows
2 parents 93c9bb9 + a65535b commit cebdfb5

File tree

2 files changed

+82
-69
lines changed

2 files changed

+82
-69
lines changed

src/main.rs

Lines changed: 80 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use iced::{
1515
widget::{button, center, container, image, row, text, Column, Text},
1616
window, Background, Border, Color, Element, Length, Shadow, Size, Task, Theme,
1717
};
18-
use platform_tools::get_mouse_position;
19-
use platform_tools::open_url;
18+
use platform_tools::{ensure_default_browser, get_mouse_position};
19+
use platform_tools::{hide_app_icon, open_url};
2020
use std::mem;
2121
use std::time::{Duration, Instant};
2222
use storage::{BrowserInfo, BrowserProfile, MatchItem, Storage};
23+
use tracing::info;
2324
use tracing_subscriber::fmt::format::FmtSpan;
2425
use url::Url;
2526

@@ -32,6 +33,7 @@ struct Gomi {
3233
keyboard: Modifiers,
3334
launch_time: Instant,
3435
stacks: Vec<Page>,
36+
current_window: Option<window::Id>,
3537
}
3638
#[derive(Debug)]
3739
enum Page {
@@ -66,10 +68,13 @@ enum Message {
6668
Back,
6769
AddProfile,
6870
TypeProfileText(String),
69-
WindowUnfocused,
7071
ShowMatchContainEditor(String, Option<String>),
7172
TypeMatchContainText(text_editor::Action),
7273
KeyboardModifiersChanged(Modifiers),
74+
OpenWindow,
75+
WindowOpened(window::Id),
76+
CloseWindow,
77+
MoveWindow(window::Id),
7378
}
7479

7580
impl Gomi {
@@ -86,10 +91,10 @@ impl Gomi {
8691
keyboard: Modifiers::default(),
8792
launch_time: Instant::now(),
8893
stacks: vec![],
94+
current_window: None,
8995
},
9096
Task::perform(
9197
async move {
92-
// hide_app_icon();
9398
let mut storage = Storage::new();
9499
let mut browsers = storage.get_browsers();
95100
if browsers.is_empty() {
@@ -108,6 +113,9 @@ impl Gomi {
108113
match message {
109114
Message::GoHome(browsers) => {
110115
self.browser_list = Some(browsers);
116+
if !ensure_default_browser() {
117+
return Task::done(Message::OpenWindow);
118+
}
111119
Task::none()
112120
}
113121
Message::LaunchBrowser(path, profile, external_operation) => {
@@ -139,45 +147,47 @@ impl Gomi {
139147
}
140148
}
141149
}
142-
return window::get_latest()
143-
.and_then(move |w| window::change_mode(w, Mode::Hidden));
150+
return Task::done(Message::CloseWindow);
144151
}
145152
Task::none()
146153
}
147154

148155
Message::ReceiveUrl(url) => {
149-
let equal_matched = storage::Storage::new().find_equal_matches_by_url(url.clone());
150156
self.current_url = Some(url.clone());
157+
let equal_matched = self.storage.find_equal_matches_by_url(url.clone());
151158
if let Some(match_item) = equal_matched {
152-
return Task::perform(
153-
async move { (match_item.browser_path, match_item.profile, None) },
154-
|v| Message::LaunchBrowser(v.0, v.1, v.2),
155-
);
159+
return Task::done(Message::LaunchBrowser(
160+
match_item.browser_path,
161+
match_item.profile,
162+
None,
163+
));
156164
}
157-
let contain_matched =
158-
storage::Storage::new().find_contain_matches_by_url(url.clone());
165+
let contain_matched = self.storage.find_contain_matches_by_url(url.clone());
159166
if let Some(match_item) = contain_matched {
160-
return Task::perform(
161-
async move { (match_item.browser_path, match_item.profile, None) },
162-
|v| Message::LaunchBrowser(v.0, v.1, v.2),
163-
);
167+
return Task::done(Message::LaunchBrowser(
168+
match_item.browser_path,
169+
match_item.profile,
170+
None,
171+
));
164172
}
165-
166-
Task::batch(vec![
167-
window::get_latest()
168-
.and_then(move |w| window::move_to(w, get_mouse_position())),
169-
window::get_latest().and_then(move |w| window::change_mode(w, Mode::Windowed)),
170-
])
173+
if let Some(window_id) = self.current_window {
174+
return Task::done(Message::MoveWindow(window_id));
175+
}
176+
Task::done(Message::OpenWindow)
171177
}
178+
Message::MoveWindow(window_id) => window::move_to(window_id, get_mouse_position()),
172179
Message::SetAsDefault => {
173180
platform_tools::set_as_default_browser();
174181
Task::none()
175182
}
176183
Message::CheckDefaultStatus => {
177184
if !self.is_default_browser {
178-
self.is_default_browser = platform_tools::ensure_default_browser();
185+
let is_default_browser = platform_tools::ensure_default_browser();
186+
self.is_default_browser = is_default_browser;
187+
if is_default_browser {
188+
return Task::done(Message::CloseWindow);
189+
}
179190
}
180-
181191
Task::none()
182192
}
183193
Message::ListProfiles(browser) => {
@@ -249,17 +259,6 @@ impl Gomi {
249259
}
250260
Task::none()
251261
}
252-
Message::WindowUnfocused => {
253-
if cfg!(debug_assertions) {
254-
Task::none()
255-
} else if self.launch_time.elapsed() < Duration::from_secs(1) {
256-
Task::none()
257-
} else {
258-
self.current_page = Page::Home;
259-
self.stacks.clear();
260-
window::get_latest().and_then(move |w| window::change_mode(w, Mode::Hidden))
261-
}
262-
}
263262
Message::ShowMatchContainEditor(browser_path, profile) => {
264263
if let Some(url) = self.current_url.clone() {
265264
let new_page = Page::MatchContainEditor {
@@ -286,10 +285,34 @@ impl Gomi {
286285
self.keyboard = modifiers;
287286
Task::none()
288287
}
288+
Message::OpenWindow => {
289+
let position = get_mouse_position();
290+
let (_, open) = window::open(window::Settings {
291+
position: Position::Specific(position),
292+
size: Size::new(WINDOW_WIDTH, WINDOW_HEIGHT),
293+
..Default::default()
294+
});
295+
open.map(Message::WindowOpened)
296+
}
297+
Message::WindowOpened(window_id) => {
298+
self.current_window = Some(window_id);
299+
Task::none()
300+
}
301+
Message::CloseWindow => {
302+
if let Some(window_id) = self.current_window {
303+
let rs = window::close(window_id);
304+
self.current_page = Page::Home;
305+
self.stacks.clear();
306+
self.current_url = None;
307+
self.current_window = None;
308+
return rs;
309+
}
310+
Task::none()
311+
}
289312
}
290313
}
291314

292-
fn view(&self) -> Element<Message> {
315+
fn view(&self, _: window::Id) -> Element<Message> {
293316
let content = match &self.current_page {
294317
Page::Home => {
295318
let browsers = self.browser_list.clone().unwrap_or_default();
@@ -382,26 +405,29 @@ impl Gomi {
382405

383406
let footer = if let Some(url) = current_url {
384407
let url_cloned = url.clone();
408+
info!("url_cloned: {}", url_cloned);
385409
let url = Url::parse(&url).unwrap();
386410
let host = url.host_str().unwrap_or_default().to_string();
387411

388412
container(tooltip(
389413
Text::new(host).size(13).style(|_| text::Style {
390414
color: Some(Color::from_rgb(0.2, 0.2, 0.2)),
391415
}),
392-
container(Text::new(url_cloned).size(13))
393-
.padding(4)
394-
.style(|_| container::Style {
395-
background: Some(Background::Color(Color::from_rgb(
396-
0.9, 0.9, 1.0,
397-
))),
398-
border: Border {
399-
radius: 4.0.into(),
400-
width: 1.0,
401-
color: Color::from_rgb(0.7, 0.7, 0.9),
402-
},
403-
..Default::default()
404-
}),
416+
container(
417+
Text::new(url_cloned)
418+
.size(13)
419+
.color(Color::from_rgb(0.2, 0.2, 0.2)),
420+
)
421+
.padding(4)
422+
.style(|_| container::Style {
423+
background: Some(Background::Color(Color::from_rgb(0.9, 0.9, 1.0))),
424+
border: Border {
425+
radius: 4.0.into(),
426+
width: 1.0,
427+
color: Color::from_rgb(0.7, 0.7, 0.9),
428+
},
429+
..Default::default()
430+
}),
405431
tooltip::Position::Top,
406432
))
407433
.padding([8, 12])
@@ -533,20 +559,19 @@ impl Gomi {
533559
content.into()
534560
}
535561

536-
fn theme(&self) -> Theme {
537-
Theme::Light
562+
fn theme(&self, window: window::Id) -> Theme {
563+
Theme::default()
538564
}
565+
539566
fn subscription(&self) -> Subscription<Message> {
540567
Subscription::batch([
541568
event::listen_url().map(Message::ReceiveUrl),
542569
iced::time::every(Duration::from_secs(1)).map(|_| Message::CheckDefaultStatus),
543570
event::listen_with(|event, _status, _window| -> Option<Message> {
544571
match event {
545-
Event::Window(window::Event::Unfocused) => Some(Message::WindowUnfocused),
546572
Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
547573
Some(Message::KeyboardModifiersChanged(modifiers))
548574
}
549-
550575
_ => None,
551576
}
552577
}),
@@ -557,22 +582,13 @@ impl Gomi {
557582
fn main() -> iced::Result {
558583
let file_appender = tracing_appender::rolling::daily(LOG_DIR, LOG_FILE);
559584
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
560-
561585
tracing_subscriber::fmt()
562586
.with_writer(non_blocking)
563587
.with_span_events(FmtSpan::CLOSE)
564588
.init();
565589

566-
iced::application("Gomi", Gomi::update, Gomi::view)
567-
.window(window::Settings {
568-
position: Position::Specific(get_mouse_position()),
569-
// decorations: false,
570-
transparent: true,
571-
resizable: false,
572-
..Default::default()
573-
})
590+
iced::daemon("Gomi", Gomi::update, Gomi::view)
574591
.font(include_bytes!("../fonts/Microns.ttf").as_slice())
575-
.window_size(Size::new(WINDOW_WIDTH, WINDOW_HEIGHT))
576592
.default_font(Font::MONOSPACE)
577593
.antialiasing(true)
578594
.theme(Gomi::theme)

src/storage.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rusqlite::Connection;
2+
use tracing::info;
23

34
use crate::config::get_db_path;
45

@@ -122,11 +123,7 @@ impl Storage {
122123
.iter()
123124
.filter(|item| url.contains(&item.match_value))
124125
.next();
125-
if matched.is_none() {
126-
None
127-
} else {
128-
Some(result[0].clone())
129-
}
126+
matched.cloned()
130127
}
131128
pub fn insert_browser_profile(&mut self, browser_profile: BrowserProfile) {
132129
self.connection

0 commit comments

Comments
 (0)