277 lines
9.9 KiB
Rust
277 lines
9.9 KiB
Rust
//! The settings dialog: the options in `settings.toml` and the context menu editor
|
|
//! (`menus.toml`). Every change is applied and stored at once.
|
|
|
|
use std::cell::{Cell, RefCell};
|
|
use std::rc::{Rc, Weak};
|
|
|
|
use slint::{ComponentHandle, ModelRc, VecModel};
|
|
|
|
use crate::actions::{Action, Actions};
|
|
use crate::config::{self, Clock, DateStyle, FrameUnit, Group, Motion, Sort, View};
|
|
use crate::folder::format;
|
|
use crate::menus::{Context, SEPARATOR};
|
|
use crate::shell::Shell;
|
|
use crate::{EditRow, MainWindow, SettingsData, theme_bridge};
|
|
|
|
pub struct SettingsUi {
|
|
me: Weak<SettingsUi>,
|
|
ui: slint::Weak<MainWindow>,
|
|
shell: Weak<Shell>,
|
|
actions: Rc<Actions>,
|
|
/// The context the menu editor shows.
|
|
context: Cell<usize>,
|
|
/// The ids of that menu: what the editor edits.
|
|
ids: RefCell<Vec<String>>,
|
|
/// Once a minute: redraws "5 minutes ago" stamps; a tick with none under an hour old
|
|
/// repaints nothing.
|
|
tick: slint::Timer,
|
|
}
|
|
|
|
impl SettingsUi {
|
|
pub fn attach(ui: &MainWindow, shell: &Rc<Shell>, actions: &Rc<Actions>) -> Rc<Self> {
|
|
let settings = Rc::new_cyclic(|me| Self {
|
|
me: me.clone(),
|
|
ui: ui.as_weak(),
|
|
shell: Rc::downgrade(shell),
|
|
actions: actions.clone(),
|
|
context: Cell::new(0),
|
|
ids: RefCell::new(Vec::new()),
|
|
tick: slint::Timer::default(),
|
|
});
|
|
let cfg = config::load::<config::Settings>("settings");
|
|
theme_bridge::set_motion(ui, cfg.motion);
|
|
format::set_dates(cfg.date_style, cfg.clock);
|
|
settings.bind(ui);
|
|
let shell = Rc::downgrade(shell);
|
|
settings.tick.start(
|
|
slint::TimerMode::Repeated,
|
|
std::time::Duration::from_secs(60),
|
|
move || {
|
|
if let Some(shell) = shell.upgrade() {
|
|
for pane in shell.panes() {
|
|
if pane.model.has_fresh_dates() {
|
|
pane.model.redraw_dates();
|
|
}
|
|
}
|
|
}
|
|
},
|
|
);
|
|
settings
|
|
}
|
|
|
|
fn data(cfg: &config::Settings) -> SettingsData {
|
|
SettingsData {
|
|
restore_session: cfg.restore_session,
|
|
show_hidden: cfg.show_hidden,
|
|
folders_first: cfg.folders_first,
|
|
view: cfg.view as i32,
|
|
sort: cfg.sort as i32,
|
|
sort_descending: cfg.sort_descending,
|
|
group: cfg.group as i32,
|
|
motion: cfg.motion as i32,
|
|
date_style: cfg.date_style as i32,
|
|
clock: cfg.clock as i32,
|
|
previews: cfg.previews,
|
|
preview_frame_unit: cfg.preview_frame_unit as i32,
|
|
preview_frame: cfg.preview_frame as i32,
|
|
preview_max_mib: cfg.preview_max_mib as i32,
|
|
preview_remote: cfg.preview_remote,
|
|
player_next: cfg.player_next,
|
|
ssh_hosts: cfg.ssh_hosts,
|
|
connect_timeout: cfg.connect_timeout_secs as i32,
|
|
keepalive: cfg.keepalive_secs as i32,
|
|
idle_close: cfg.idle_close_mins as i32,
|
|
}
|
|
}
|
|
|
|
fn opening(&self) {
|
|
let Some(ui) = self.ui.upgrade() else {
|
|
return;
|
|
};
|
|
ui.set_settings(Self::data(&config::load("settings")));
|
|
ui.set_menu_contexts(ModelRc::new(VecModel::from(
|
|
Context::ALL
|
|
.iter()
|
|
.map(|c| c.label().into())
|
|
.collect::<Vec<slint::SharedString>>(),
|
|
)));
|
|
self.show_menu(self.context.get().min(Context::ALL.len() - 1));
|
|
}
|
|
|
|
fn changed(&self, data: SettingsData) {
|
|
let mut cfg: config::Settings = config::load("settings");
|
|
cfg.restore_session = data.restore_session;
|
|
cfg.show_hidden = data.show_hidden;
|
|
cfg.folders_first = data.folders_first;
|
|
cfg.view = [View::Details, View::Grid][data.view.clamp(0, 1) as usize];
|
|
cfg.sort =
|
|
[Sort::Name, Sort::Size, Sort::Type, Sort::Modified][data.sort.clamp(0, 3) as usize];
|
|
cfg.sort_descending = data.sort_descending;
|
|
cfg.group = [Group::None, Group::Type, Group::Modified, Group::Size]
|
|
[data.group.clamp(0, 3) as usize];
|
|
cfg.motion =
|
|
[Motion::System, Motion::Reduce, Motion::Full][data.motion.clamp(0, 2) as usize];
|
|
cfg.date_style =
|
|
[DateStyle::Relative, DateStyle::Absolute][data.date_style.clamp(0, 1) as usize];
|
|
cfg.clock = [Clock::H12, Clock::H24][data.clock.clamp(0, 1) as usize];
|
|
cfg.previews = data.previews;
|
|
cfg.preview_frame_unit =
|
|
[FrameUnit::Percent, FrameUnit::Seconds][data.preview_frame_unit.clamp(0, 1) as usize];
|
|
cfg.preview_frame = data.preview_frame.clamp(0, 3600) as u32;
|
|
cfg.preview_max_mib = data.preview_max_mib.clamp(1, 4096) as u32;
|
|
cfg.preview_remote = data.preview_remote;
|
|
cfg.player_next = data.player_next;
|
|
cfg.ssh_hosts = data.ssh_hosts;
|
|
cfg.connect_timeout_secs = data.connect_timeout.clamp(5, 120) as u32;
|
|
cfg.keepalive_secs = data.keepalive.clamp(0, 300) as u32;
|
|
cfg.idle_close_mins = data.idle_close.clamp(1, 120) as u32;
|
|
// The window's own sidebar state is saved with the session, not by this dialog.
|
|
if let Some(ui) = self.ui.upgrade() {
|
|
cfg.show_sidebar = ui.get_show_sidebar();
|
|
theme_bridge::set_motion(&ui, cfg.motion);
|
|
}
|
|
config::store("settings", &cfg);
|
|
format::set_dates(cfg.date_style, cfg.clock);
|
|
if let Some(shell) = self.shell.upgrade() {
|
|
shell.thumbs().configure(&cfg);
|
|
crate::net_bridge::configure(&shell, &cfg);
|
|
for pane in shell.panes() {
|
|
pane.model.apply_config(&cfg);
|
|
pane.model.redraw_dates();
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- menu editor --------------------------------------------------------------------
|
|
|
|
fn context(&self) -> Context {
|
|
Context::ALL[self.context.get().min(Context::ALL.len() - 1)]
|
|
}
|
|
|
|
fn row(id: &str) -> Option<EditRow> {
|
|
if id == SEPARATOR {
|
|
return Some(EditRow {
|
|
text: Default::default(),
|
|
icon: Default::default(),
|
|
shortcut: Default::default(),
|
|
separator: true,
|
|
});
|
|
}
|
|
let action = Action::from_id(id)?;
|
|
Some(EditRow {
|
|
text: action.label().into(),
|
|
icon: action.glyph().into(),
|
|
shortcut: action.shortcut_text().into(),
|
|
separator: false,
|
|
})
|
|
}
|
|
|
|
/// Shows the menu of context `index`.
|
|
fn show_menu(&self, index: usize) {
|
|
self.context.set(index);
|
|
let ctx = self.context();
|
|
// Ids this build doesn't know are not shown, so an edit drops them.
|
|
let ids: Vec<String> = self
|
|
.actions
|
|
.menu_ids(ctx)
|
|
.into_iter()
|
|
.filter(|id| id == SEPARATOR || Action::from_id(id).is_some())
|
|
.collect();
|
|
*self.ids.borrow_mut() = ids;
|
|
self.refresh();
|
|
}
|
|
|
|
fn refresh(&self) {
|
|
let Some(ui) = self.ui.upgrade() else {
|
|
return;
|
|
};
|
|
let ids = self.ids.borrow();
|
|
let rows: Vec<EditRow> = ids.iter().filter_map(|id| Self::row(id)).collect();
|
|
let available: Vec<EditRow> = Action::ALL
|
|
.iter()
|
|
.filter(|a| !ids.iter().any(|id| id == a.id()))
|
|
.filter_map(|a| Self::row(a.id()))
|
|
.collect();
|
|
ui.set_menu_context(self.context.get() as i32);
|
|
ui.set_menu_rows(ModelRc::new(VecModel::from(rows)));
|
|
ui.set_menu_available(ModelRc::new(VecModel::from(available)));
|
|
ui.set_menu_can_reset(!self.actions.menu_is_default(self.context()));
|
|
}
|
|
|
|
/// Stores the edited ids and shows them.
|
|
fn commit(&self) {
|
|
self.actions
|
|
.set_menu(self.context(), Some(self.ids.borrow().clone()));
|
|
self.refresh();
|
|
}
|
|
|
|
fn moved(&self, from: usize, to: usize) {
|
|
{
|
|
let mut ids = self.ids.borrow_mut();
|
|
if from >= ids.len() || to >= ids.len() {
|
|
return;
|
|
}
|
|
let id = ids.remove(from);
|
|
ids.insert(to, id);
|
|
}
|
|
self.commit();
|
|
}
|
|
|
|
fn removed(&self, index: usize) {
|
|
{
|
|
let mut ids = self.ids.borrow_mut();
|
|
if index >= ids.len() {
|
|
return;
|
|
}
|
|
ids.remove(index);
|
|
}
|
|
self.commit();
|
|
}
|
|
|
|
/// Adds the `index`th of the actions not in the menu.
|
|
fn added(&self, index: usize) {
|
|
let id = {
|
|
let ids = self.ids.borrow();
|
|
Action::ALL
|
|
.iter()
|
|
.filter(|a| !ids.iter().any(|id| id == a.id()))
|
|
.nth(index)
|
|
.map(|a| a.id().to_string())
|
|
};
|
|
if let Some(id) = id {
|
|
self.ids.borrow_mut().push(id);
|
|
self.commit();
|
|
}
|
|
}
|
|
|
|
fn reset(&self) {
|
|
self.actions.set_menu(self.context(), None);
|
|
self.show_menu(self.context.get());
|
|
}
|
|
|
|
fn bind(&self, ui: &MainWindow) {
|
|
macro_rules! on {
|
|
($setter:ident, |$s:ident $(, $arg:ident)*| $body:expr) => {{
|
|
let me = self.me.clone();
|
|
ui.$setter(move |$($arg),*| {
|
|
if let Some($s) = me.upgrade() {
|
|
$body
|
|
}
|
|
});
|
|
}};
|
|
}
|
|
on!(on_settings_opening, |s| s.opening());
|
|
on!(on_settings_changed, |s, data| s.changed(data));
|
|
on!(on_menu_context_selected, |s, i| s
|
|
.show_menu(i.max(0) as usize));
|
|
on!(on_menu_moved, |s, from, to| s
|
|
.moved(from.max(0) as usize, to.max(0) as usize));
|
|
on!(on_menu_removed, |s, i| s.removed(i.max(0) as usize));
|
|
on!(on_menu_added, |s, i| s.added(i.max(0) as usize));
|
|
on!(on_menu_separator_added, |s| {
|
|
s.ids.borrow_mut().push(SEPARATOR.to_string());
|
|
s.commit();
|
|
});
|
|
on!(on_menu_reset, |s| s.reset());
|
|
}
|
|
}
|