Add configurable popup settings
This commit is contained in:
@@ -2,7 +2,7 @@ use anyhow::{Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fs, os::unix::fs::PermissionsExt, path::PathBuf};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
pub history_limit: usize,
|
||||
@@ -11,7 +11,9 @@ pub struct Config {
|
||||
pub history_retention_days: u64,
|
||||
pub global_shortcut: String,
|
||||
pub autostart: bool,
|
||||
pub always_on_top: bool,
|
||||
pub show_window_after_unlock: bool,
|
||||
pub inactive_opacity: f64,
|
||||
pub log_level: String,
|
||||
}
|
||||
|
||||
@@ -24,7 +26,9 @@ impl Default for Config {
|
||||
history_retention_days: 30,
|
||||
global_shortcut: "Ctrl+Alt+V".into(),
|
||||
autostart: true,
|
||||
always_on_top: true,
|
||||
show_window_after_unlock: true,
|
||||
inactive_opacity: 0.60,
|
||||
log_level: "info".into(),
|
||||
}
|
||||
}
|
||||
@@ -97,6 +101,9 @@ impl Config {
|
||||
if self.history_retention_days == 0 {
|
||||
anyhow::bail!("history_retention_days must be greater than zero")
|
||||
}
|
||||
if !self.inactive_opacity.is_finite() || !(0.0..=1.0).contains(&self.inactive_opacity) {
|
||||
anyhow::bail!("inactive_opacity must be between 0 and 1")
|
||||
}
|
||||
if !matches!(
|
||||
self.log_level.to_ascii_lowercase().as_str(),
|
||||
"trace" | "debug" | "info" | "warn" | "error"
|
||||
@@ -154,7 +161,9 @@ mod tests {
|
||||
assert_eq!(c.max_entry_bytes, 1_048_576);
|
||||
assert_eq!(c.history_retention_days, 30);
|
||||
assert_eq!(c.global_shortcut, "Ctrl+Alt+V");
|
||||
assert!(c.always_on_top);
|
||||
assert!(c.show_window_after_unlock);
|
||||
assert_eq!(c.inactive_opacity, 0.60);
|
||||
assert!(c.validate().is_ok());
|
||||
}
|
||||
|
||||
@@ -180,4 +189,15 @@ mod tests {
|
||||
assert!(config.validate().is_ok(), "{shortcut}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_inactive_opacity() {
|
||||
for inactive_opacity in [-0.01, 1.01, f64::NAN, f64::INFINITY] {
|
||||
let config = Config {
|
||||
inactive_opacity,
|
||||
..Config::default()
|
||||
};
|
||||
assert!(config.validate().is_err(), "{inactive_opacity}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
123
src/gui.rs
123
src/gui.rs
@@ -13,7 +13,7 @@ use anyhow::{Context, Result};
|
||||
use gtk4::prelude::*;
|
||||
use gtk4::{
|
||||
Application, ApplicationWindow, Box as GtkBox, Button, CheckButton, Entry, Label, ListBox,
|
||||
ListBoxRow, Orientation, ResponseType, ScrolledWindow, TextView,
|
||||
ListBoxRow, Orientation, ResponseType, Scale, ScrolledWindow, SpinButton, TextView,
|
||||
};
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
@@ -358,6 +358,8 @@ fn build_window(
|
||||
.default_height(420)
|
||||
.child(&root)
|
||||
.build();
|
||||
let inactive_opacity = Rc::new(std::cell::Cell::new(config.inactive_opacity));
|
||||
let always_on_top = Rc::new(std::cell::Cell::new(config.always_on_top));
|
||||
let css = gtk4::CssProvider::new();
|
||||
css.load_from_data(
|
||||
"button.paused { background-color: #f3a6a6; color: #3d0000; font-weight: bold; } row.pinned { background-color: rgba(79, 107, 237, 0.10); border-radius: 6px; }",
|
||||
@@ -370,20 +372,40 @@ fn build_window(
|
||||
);
|
||||
}
|
||||
let window_for_opacity = window.clone();
|
||||
let inactive_opacity_for_realize = Rc::clone(&inactive_opacity);
|
||||
let always_on_top_for_realize = Rc::clone(&always_on_top);
|
||||
window.connect_realize(move |_| {
|
||||
crate::platform::linux::x11_window::configure(&window_for_opacity);
|
||||
crate::platform::linux::x11_window::configure(
|
||||
&window_for_opacity,
|
||||
inactive_opacity_for_realize.get(),
|
||||
always_on_top_for_realize.get(),
|
||||
);
|
||||
});
|
||||
let window_for_above = window.clone();
|
||||
let inactive_opacity_for_map = Rc::clone(&inactive_opacity);
|
||||
let always_on_top_for_map = Rc::clone(&always_on_top);
|
||||
window.connect_map(move |_| {
|
||||
crate::platform::linux::x11_window::configure(&window_for_above);
|
||||
crate::platform::linux::x11_window::configure(
|
||||
&window_for_above,
|
||||
inactive_opacity_for_map.get(),
|
||||
always_on_top_for_map.get(),
|
||||
);
|
||||
});
|
||||
let window_for_opacity = window.clone();
|
||||
let inactive_opacity_for_active = Rc::clone(&inactive_opacity);
|
||||
window.connect_is_active_notify(move |_| {
|
||||
crate::platform::linux::x11_window::set_active_opacity(&window_for_opacity);
|
||||
crate::platform::linux::x11_window::set_active_opacity(
|
||||
&window_for_opacity,
|
||||
inactive_opacity_for_active.get(),
|
||||
);
|
||||
});
|
||||
let window_for_opacity = window.clone();
|
||||
let inactive_opacity_for_timeout = Rc::clone(&inactive_opacity);
|
||||
glib::timeout_add_local(std::time::Duration::from_millis(100), move || {
|
||||
crate::platform::linux::x11_window::set_active_opacity(&window_for_opacity);
|
||||
crate::platform::linux::x11_window::set_active_opacity(
|
||||
&window_for_opacity,
|
||||
inactive_opacity_for_timeout.get(),
|
||||
);
|
||||
glib::ControlFlow::Continue
|
||||
});
|
||||
let recording = state.borrow().recording;
|
||||
@@ -701,13 +723,17 @@ fn build_window(
|
||||
});
|
||||
|
||||
let window_for_settings = window.clone();
|
||||
let config_for_settings = config.clone();
|
||||
let config_for_settings = Rc::new(RefCell::new(config.clone()));
|
||||
let status_for_settings = status.clone();
|
||||
let inactive_opacity_for_settings = Rc::clone(&inactive_opacity);
|
||||
let always_on_top_for_settings = Rc::clone(&always_on_top);
|
||||
settings_button.connect_clicked(move |_| {
|
||||
show_settings(
|
||||
&window_for_settings,
|
||||
&config_for_settings,
|
||||
Rc::clone(&config_for_settings),
|
||||
&status_for_settings,
|
||||
Rc::clone(&inactive_opacity_for_settings),
|
||||
Rc::clone(&always_on_top_for_settings),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1096,7 +1122,14 @@ fn show_text_dialog(window: &ApplicationWindow, title: &str, content: &str) {
|
||||
dialog.present();
|
||||
}
|
||||
|
||||
fn show_settings(window: &ApplicationWindow, config: &Config, status: &Label) {
|
||||
fn show_settings(
|
||||
window: &ApplicationWindow,
|
||||
config: Rc<RefCell<Config>>,
|
||||
status: &Label,
|
||||
inactive_opacity: Rc<std::cell::Cell<f64>>,
|
||||
always_on_top: Rc<std::cell::Cell<bool>>,
|
||||
) {
|
||||
let current_config = config.borrow().clone();
|
||||
let dialog = gtk4::Dialog::builder()
|
||||
.transient_for(window)
|
||||
.modal(true)
|
||||
@@ -1109,24 +1142,88 @@ fn show_settings(window: &ApplicationWindow, config: &Config, status: &Label) {
|
||||
content.set_margin_start(18);
|
||||
content.set_margin_end(18);
|
||||
let show_window = CheckButton::with_label("Show the Cliphoard window after unlock");
|
||||
show_window.set_active(config.show_window_after_unlock);
|
||||
show_window.set_active(current_config.show_window_after_unlock);
|
||||
content.append(&show_window);
|
||||
let keep_on_top = CheckButton::with_label("Always keep the popup in the foreground");
|
||||
keep_on_top.set_active(always_on_top.get());
|
||||
content.append(&keep_on_top);
|
||||
let explanation = Label::new(Some(
|
||||
"When disabled, Cliphoard starts in the tray after you enter the passphrase. You can still open the window with the global shortcut.",
|
||||
"When disabled, Cliphoard starts in the tray after you enter the passphrase. You can still open the window with the global shortcut. Shortcut, autostart, and history-limit changes apply after restart.",
|
||||
));
|
||||
explanation.set_wrap(true);
|
||||
content.append(&explanation);
|
||||
let opacity_label = Label::new(Some("Opacity when unfocused"));
|
||||
content.append(&opacity_label);
|
||||
let opacity = Scale::with_range(Orientation::Horizontal, 0.0, 100.0, 1.0);
|
||||
opacity.set_value(inactive_opacity.get() * 100.0);
|
||||
opacity.set_digits(0);
|
||||
opacity.set_draw_value(true);
|
||||
opacity.set_hexpand(true);
|
||||
opacity.set_tooltip_text(Some("How transparent the popup is when it is not focused"));
|
||||
content.append(&opacity);
|
||||
let shortcut_label = Label::new(Some("Global shortcut (applies after restart)"));
|
||||
content.append(&shortcut_label);
|
||||
let shortcut = Entry::new();
|
||||
shortcut.set_text(¤t_config.global_shortcut);
|
||||
shortcut.set_tooltip_text(Some("For example: Ctrl+Alt+V"));
|
||||
content.append(&shortcut);
|
||||
let autostart = CheckButton::with_label("Start Cliphoard automatically when I log in");
|
||||
autostart.set_active(current_config.autostart);
|
||||
content.append(&autostart);
|
||||
let history_limit_label = Label::new(Some("Maximum unpinned history entries"));
|
||||
content.append(&history_limit_label);
|
||||
let history_limit = SpinButton::with_range(1.0, 10_000.0, 1.0);
|
||||
history_limit.set_value(current_config.history_limit as f64);
|
||||
content.append(&history_limit);
|
||||
let pinned_limit_label = Label::new(Some("Maximum pinned entries"));
|
||||
content.append(&pinned_limit_label);
|
||||
let pinned_limit = SpinButton::with_range(1.0, 10_000.0, 1.0);
|
||||
pinned_limit.set_value(current_config.pinned_limit as f64);
|
||||
content.append(&pinned_limit);
|
||||
let retention_label = Label::new(Some("Remove unpinned entries older than (days)"));
|
||||
content.append(&retention_label);
|
||||
let retention_days = SpinButton::with_range(1.0, 3_650.0, 1.0);
|
||||
retention_days.set_value(current_config.history_retention_days as f64);
|
||||
content.append(&retention_days);
|
||||
dialog.content_area().append(&content);
|
||||
dialog.add_button("Cancel", ResponseType::Cancel);
|
||||
dialog.add_button("Save", ResponseType::Accept);
|
||||
let config_for_response = config.clone();
|
||||
let config_for_response = Rc::clone(&config);
|
||||
let status_for_response = status.clone();
|
||||
let window_for_response = window.clone();
|
||||
dialog.connect_response(move |dialog, response| {
|
||||
if response == ResponseType::Accept {
|
||||
let mut updated_config = config_for_response.clone();
|
||||
let mut updated_config = config_for_response.borrow().clone();
|
||||
updated_config.show_window_after_unlock = show_window.is_active();
|
||||
updated_config.always_on_top = keep_on_top.is_active();
|
||||
updated_config.inactive_opacity = opacity.value() / 100.0;
|
||||
updated_config.global_shortcut = shortcut.text().to_string();
|
||||
updated_config.autostart = autostart.is_active();
|
||||
updated_config.history_limit = history_limit.value_as_int() as usize;
|
||||
updated_config.pinned_limit = pinned_limit.value_as_int() as usize;
|
||||
updated_config.history_retention_days = retention_days.value_as_int() as u64;
|
||||
if let Err(error) = updated_config.validate() {
|
||||
set_status(
|
||||
&status_for_response,
|
||||
&format!("Could not save settings: {error}"),
|
||||
);
|
||||
return;
|
||||
}
|
||||
match updated_config.save() {
|
||||
Ok(()) => set_status(&status_for_response, "Settings saved"),
|
||||
Ok(()) => {
|
||||
*config_for_response.borrow_mut() = updated_config.clone();
|
||||
inactive_opacity.set(updated_config.inactive_opacity);
|
||||
always_on_top.set(updated_config.always_on_top);
|
||||
crate::platform::linux::x11_window::set_active_opacity(
|
||||
&window_for_response,
|
||||
updated_config.inactive_opacity,
|
||||
);
|
||||
crate::platform::linux::x11_window::set_always_on_top(
|
||||
&window_for_response,
|
||||
updated_config.always_on_top,
|
||||
);
|
||||
set_status(&status_for_response, "Settings saved");
|
||||
}
|
||||
Err(error) => set_status(
|
||||
&status_for_response,
|
||||
&format!("Could not save settings: {error}"),
|
||||
|
||||
@@ -9,10 +9,13 @@ use x11rb::{
|
||||
};
|
||||
|
||||
const ACTIVE_OPACITY: f64 = 1.0;
|
||||
const INACTIVE_OPACITY: f64 = 0.60;
|
||||
|
||||
pub fn configure(window: &ApplicationWindow) {
|
||||
set_active_opacity(window);
|
||||
pub fn configure(window: &ApplicationWindow, inactive_opacity: f64, always_on_top: bool) {
|
||||
set_active_opacity(window, inactive_opacity);
|
||||
set_always_on_top(window, always_on_top);
|
||||
}
|
||||
|
||||
pub fn set_always_on_top(window: &ApplicationWindow, always_on_top: bool) {
|
||||
let Some(surface) = window.surface() else {
|
||||
return;
|
||||
};
|
||||
@@ -37,15 +40,6 @@ pub fn configure(window: &ApplicationWindow) {
|
||||
let Ok(above) = above_cookie.reply() else {
|
||||
return;
|
||||
};
|
||||
if let Err(error) = connection.change_property32(
|
||||
x11rb::protocol::xproto::PropMode::REPLACE,
|
||||
xid,
|
||||
state.atom,
|
||||
x11rb::protocol::xproto::AtomEnum::ATOM,
|
||||
&[above.atom],
|
||||
) {
|
||||
tracing::warn!(%error, "could not mark popup as always on top");
|
||||
}
|
||||
let root = connection.setup().roots[0].root;
|
||||
let event = ClientMessageEvent {
|
||||
response_type: CLIENT_MESSAGE_EVENT,
|
||||
@@ -53,7 +47,7 @@ pub fn configure(window: &ApplicationWindow) {
|
||||
sequence: 0,
|
||||
window: xid,
|
||||
type_: state.atom,
|
||||
data: ClientMessageData::from([1, above.atom, 0, 0, 0]),
|
||||
data: ClientMessageData::from([if always_on_top { 1 } else { 0 }, above.atom, 0, 0, 0]),
|
||||
};
|
||||
match connection.send_event(
|
||||
false,
|
||||
@@ -63,15 +57,17 @@ pub fn configure(window: &ApplicationWindow) {
|
||||
) {
|
||||
Ok(cookie) => {
|
||||
if let Err(error) = cookie.check() {
|
||||
tracing::debug!(%error, "window manager rejected always-on-top request");
|
||||
tracing::debug!(%error, always_on_top, "window manager rejected always-on-top request");
|
||||
}
|
||||
}
|
||||
Err(error) => tracing::debug!(%error, "could not send always-on-top request"),
|
||||
Err(error) => {
|
||||
tracing::debug!(%error, always_on_top, "could not send always-on-top request")
|
||||
}
|
||||
}
|
||||
let _ = connection.flush();
|
||||
}
|
||||
|
||||
pub fn set_active_opacity(window: &ApplicationWindow) {
|
||||
pub fn set_active_opacity(window: &ApplicationWindow, inactive_opacity: f64) {
|
||||
let Some(surface) = window.surface() else {
|
||||
return;
|
||||
};
|
||||
@@ -90,7 +86,7 @@ pub fn set_active_opacity(window: &ApplicationWindow) {
|
||||
let value = if window.is_active() {
|
||||
ACTIVE_OPACITY
|
||||
} else {
|
||||
INACTIVE_OPACITY
|
||||
inactive_opacity
|
||||
};
|
||||
let scaled = (value * u32::MAX as f64).round() as u32;
|
||||
if let Err(error) = connection.change_property32(
|
||||
|
||||
Reference in New Issue
Block a user