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
73 changes: 63 additions & 10 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ fn push_toast(toasts: &mut Toasts, toast: Toast) {
toasts.add(toast);
}

fn normalize_static_window_config(
follow_mouse: bool,
static_location_enabled: bool,
static_pos: Option<(i32, i32)>,
static_size: Option<(i32, i32)>,
) -> (bool, Option<(i32, i32)>, Option<(i32, i32)>) {
if follow_mouse {
(false, None, None)
} else {
(static_location_enabled, static_pos, static_size)
}
}

static APP_EVENT_TXS: Lazy<Mutex<Vec<Sender<WatchEvent>>>> = Lazy::new(|| Mutex::new(Vec::new()));

pub fn register_event_sender(tx: Sender<WatchEvent>) {
Expand Down Expand Up @@ -887,15 +900,19 @@ impl LauncherApp {
if let Some(v) = follow_mouse {
self.follow_mouse = v;
}
if let Some(v) = static_enabled {
self.static_location_enabled = v;
}
if static_pos.is_some() {
self.static_pos = static_pos;
}
if static_size.is_some() {
self.static_size = static_size;
}
let requested_static_enabled = static_enabled.unwrap_or(self.static_location_enabled);
let requested_static_pos = static_pos.or(self.static_pos);
let requested_static_size = static_size.or(self.static_size);
let (normalized_static_enabled, normalized_static_pos, normalized_static_size) =
normalize_static_window_config(
self.follow_mouse,
requested_static_enabled,
requested_static_pos,
requested_static_size,
);
self.static_location_enabled = normalized_static_enabled;
self.static_pos = normalized_static_pos;
self.static_size = normalized_static_size;
if let Some(v) = hide_after_run {
self.hide_after_run = v;
}
Expand Down Expand Up @@ -1222,7 +1239,12 @@ impl LauncherApp {
let static_pos = settings.static_pos;
let static_size = settings.static_size;
let follow_mouse = settings.follow_mouse;
let static_enabled = settings.static_location_enabled;
let (static_enabled, static_pos, static_size) = normalize_static_window_config(
follow_mouse,
settings.static_location_enabled,
static_pos,
static_size,
);

let note_external_open = settings
.plugin_settings
Expand Down Expand Up @@ -5434,4 +5456,35 @@ mod tests {
NOTE_SEARCH_DEBOUNCE,
));
}
#[test]
fn launcher_new_normalizes_conflicting_follow_mouse_static_config() {
let ctx = egui::Context::default();
let mut settings = Settings::default();
settings.follow_mouse = true;
settings.static_location_enabled = true;
settings.static_pos = Some((10, 20));
settings.static_size = Some((500, 400));

let app = LauncherApp::new(
&ctx,
Arc::new(Vec::new()),
0,
PluginManager::new(),
"actions.json".into(),
"settings.json".into(),
settings,
None,
None,
None,
None,
Arc::new(AtomicBool::new(false)),
Arc::new(AtomicBool::new(false)),
Arc::new(AtomicBool::new(false)),
);

assert!(app.follow_mouse);
assert!(!app.static_location_enabled);
assert_eq!(app.static_pos, None);
assert_eq!(app.static_size, None);
}
}
71 changes: 64 additions & 7 deletions src/settings_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ pub struct SettingsEditor {
}

impl SettingsEditor {
fn normalized_static_settings(
follow_mouse: bool,
static_enabled: bool,
static_pos: (i32, i32),
static_size: (i32, i32),
) -> (bool, Option<(i32, i32)>, Option<(i32, i32)>) {
if follow_mouse {
(false, None, None)
} else {
(static_enabled, Some(static_pos), Some(static_size))
}
}

pub fn new(settings: &Settings) -> Self {
let hotkey = settings.hotkey.clone().unwrap_or_default();
let hotkey_valid = parse_hotkey(&hotkey).is_some();
Expand Down Expand Up @@ -106,6 +119,13 @@ impl SettingsEditor {
} else {
String::new()
};
let follow_mouse = settings.follow_mouse;
let (static_enabled, _, _) = Self::normalized_static_settings(
follow_mouse,
settings.static_location_enabled,
settings.static_pos.unwrap_or((0, 0)),
settings.static_size.unwrap_or((400, 220)),
);
let mut s = Self {
hotkey,
hotkey_valid,
Expand Down Expand Up @@ -138,8 +158,8 @@ impl SettingsEditor {
fuzzy_weight: settings.fuzzy_weight,
usage_weight: settings.usage_weight,
page_jump: settings.page_jump,
follow_mouse: settings.follow_mouse,
static_enabled: settings.static_location_enabled,
follow_mouse,
static_enabled,
static_x: settings.static_pos.unwrap_or((0, 0)).0,
static_y: settings.static_pos.unwrap_or((0, 0)).1,
static_w: settings.static_size.unwrap_or((400, 220)).0,
Expand Down Expand Up @@ -241,6 +261,12 @@ impl SettingsEditor {
}

pub fn to_settings(&self, current: &Settings) -> Settings {
let (static_location_enabled, static_pos, static_size) = Self::normalized_static_settings(
self.follow_mouse,
self.static_enabled,
(self.static_x, self.static_y),
(self.static_w, self.static_h),
);
Settings {
hotkey: if self.hotkey.trim().is_empty() {
None
Expand Down Expand Up @@ -280,9 +306,9 @@ impl SettingsEditor {
usage_weight: self.usage_weight,
page_jump: self.page_jump,
follow_mouse: self.follow_mouse,
static_location_enabled: self.static_enabled,
static_pos: Some((self.static_x, self.static_y)),
static_size: Some((self.static_w, self.static_h)),
static_location_enabled,
static_pos,
static_size,
hide_after_run: self.hide_after_run,
always_on_top: self.always_on_top,
timer_refresh: self.timer_refresh,
Expand Down Expand Up @@ -485,11 +511,14 @@ impl SettingsEditor {
ui.add(egui::DragValue::new(&mut self.note_panel_h));
});

ui.checkbox(&mut self.follow_mouse, "Follow mouse");
let follow_mouse_resp = ui.checkbox(&mut self.follow_mouse, "Follow mouse");
if follow_mouse_resp.changed() && self.follow_mouse {
self.static_enabled = false;
}
ui.add_enabled_ui(!self.follow_mouse, |ui| {
ui.checkbox(&mut self.static_enabled, "Use static position");
});
if self.static_enabled {
if !self.follow_mouse && self.static_enabled {
ui.horizontal(|ui| {
ui.label("X");
ui.add(egui::DragValue::new(&mut self.static_x));
Expand Down Expand Up @@ -894,3 +923,31 @@ impl SettingsEditor {
app.show_settings = open;
}
}

#[cfg(test)]
mod tests {
use super::SettingsEditor;
use crate::settings::Settings;

#[test]
fn to_settings_disables_static_when_follow_mouse_enabled() {
let mut initial = Settings::default();
initial.static_location_enabled = true;
initial.static_pos = Some((100, 200));
initial.static_size = Some((900, 700));

let mut editor = SettingsEditor::new(&initial);
editor.follow_mouse = true;
editor.static_enabled = true;
editor.static_x = 1;
editor.static_y = 2;
editor.static_w = 3;
editor.static_h = 4;

let saved = editor.to_settings(&initial);
assert!(saved.follow_mouse);
assert!(!saved.static_location_enabled);
assert_eq!(saved.static_pos, None);
assert_eq!(saved.static_size, None);
}
}