826 lines
28 KiB
Rust
826 lines
28 KiB
Rust
//! The navigation shell: tabs of one or two panes, history, the breadcrumb, the places
|
|
//! sidebar and the status bar, bound to the `MainWindow`.
|
|
|
|
pub(crate) mod pane;
|
|
mod path;
|
|
pub mod places;
|
|
|
|
use std::cell::{Cell, OnceCell, RefCell};
|
|
use std::rc::{Rc, Weak};
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use slint::{ComponentHandle, Model, ModelRc, VecModel};
|
|
use tunal_core::{Locations, Uri};
|
|
|
|
use crate::actions::Actions;
|
|
use crate::config::{self, Session, TabSession};
|
|
use crate::folder::format;
|
|
use crate::{
|
|
Completion, Crumb, MainWindow, MenuEntry, MenuItem, PaneData, PlaceItem, SidebarSection,
|
|
TabData,
|
|
};
|
|
use pane::Pane;
|
|
use places::{Places, Section};
|
|
|
|
struct Tab {
|
|
id: i32,
|
|
slot: usize,
|
|
panes: Vec<Rc<Pane>>,
|
|
active: usize,
|
|
}
|
|
|
|
/// What the place menu was opened on.
|
|
#[derive(Clone, Copy)]
|
|
struct MenuTarget {
|
|
section: Section,
|
|
index: usize,
|
|
}
|
|
|
|
pub struct Shell {
|
|
ui: slint::Weak<MainWindow>,
|
|
me: Weak<Shell>,
|
|
locations: Locations,
|
|
blocking: bool,
|
|
/// Tabs in opening order, as the tab model rows.
|
|
tabs: RefCell<Vec<Tab>>,
|
|
current: Cell<usize>,
|
|
next_id: Cell<u64>,
|
|
tab_rows: Rc<VecModel<TabData>>,
|
|
pane_rows: Rc<VecModel<PaneData>>,
|
|
places: RefCell<Places>,
|
|
menu_target: Cell<Option<MenuTarget>>,
|
|
/// Bumped per completion request; older results are dropped.
|
|
completion: Arc<AtomicU64>,
|
|
/// The file actions; set once the shell exists.
|
|
actions: OnceCell<Rc<Actions>>,
|
|
}
|
|
|
|
impl Shell {
|
|
/// Binds the shell to `ui` and opens `tabs` (one or two folders each; the last one is
|
|
/// current), else the saved session, else home. `blocking` lists folders before
|
|
/// returning (screenshots).
|
|
pub fn attach(
|
|
ui: &MainWindow,
|
|
locations: Locations,
|
|
tabs: Vec<Vec<Uri>>,
|
|
blocking: bool,
|
|
) -> Rc<Self> {
|
|
let shell = Rc::new_cyclic(|me| Self {
|
|
ui: ui.as_weak(),
|
|
me: me.clone(),
|
|
locations,
|
|
blocking,
|
|
tabs: RefCell::new(Vec::new()),
|
|
current: Cell::new(0),
|
|
next_id: Cell::new(1),
|
|
tab_rows: Rc::new(VecModel::default()),
|
|
pane_rows: Rc::new(VecModel::default()),
|
|
places: RefCell::new(Places::load()),
|
|
menu_target: Cell::new(None),
|
|
completion: Arc::new(AtomicU64::new(0)),
|
|
actions: OnceCell::new(),
|
|
});
|
|
let _ = shell
|
|
.actions
|
|
.set(Actions::attach(ui, &shell, shell.locations.clone()));
|
|
ui.set_tabs(ModelRc::from(shell.tab_rows.clone()));
|
|
ui.set_panes(ModelRc::from(shell.pane_rows.clone()));
|
|
let settings: config::Settings = config::load("settings");
|
|
ui.set_show_sidebar(settings.show_sidebar);
|
|
shell.bind(ui);
|
|
|
|
let session: Session = if tabs.is_empty() && settings.restore_session {
|
|
config::load("session")
|
|
} else {
|
|
Session::default()
|
|
};
|
|
if !tabs.is_empty() {
|
|
for dirs in tabs {
|
|
shell.open_tab(dirs);
|
|
}
|
|
} else {
|
|
for tab in &session.tabs {
|
|
let uris: Vec<Uri> = tab
|
|
.panes
|
|
.iter()
|
|
.filter_map(|u| Uri::parse(u))
|
|
.take(2)
|
|
.collect();
|
|
if !uris.is_empty() {
|
|
shell.open_tab(uris);
|
|
let mut tabs = shell.tabs.borrow_mut();
|
|
let t = tabs.last_mut().expect("just opened");
|
|
t.active = tab.active.min(t.panes.len() - 1);
|
|
}
|
|
}
|
|
if shell.tabs.borrow().is_empty() {
|
|
shell.open_tab(vec![Uri::from_path(&places::home())]);
|
|
} else {
|
|
let n = shell.tabs.borrow().len();
|
|
shell.current.set(session.current.min(n - 1));
|
|
}
|
|
}
|
|
shell.show_current();
|
|
shell
|
|
}
|
|
|
|
/// The open tabs, for `session.toml`, and the sidebar setting, for `settings.toml`.
|
|
pub fn save(&self) {
|
|
let tabs = self.tabs.borrow();
|
|
let mut order: Vec<&Tab> = tabs.iter().collect();
|
|
order.sort_by_key(|t| t.slot);
|
|
let session = Session {
|
|
tabs: order
|
|
.iter()
|
|
.map(|t| TabSession {
|
|
panes: t
|
|
.panes
|
|
.iter()
|
|
.filter_map(|p| p.dir())
|
|
.map(|d| d.to_string())
|
|
.collect(),
|
|
active: t.active,
|
|
})
|
|
.collect(),
|
|
current: tabs.get(self.current.get()).map_or(0, |t| t.slot),
|
|
};
|
|
config::store("session", &session);
|
|
let mut settings: config::Settings = config::load("settings");
|
|
if let Some(ui) = self.ui.upgrade() {
|
|
settings.show_sidebar = ui.get_show_sidebar();
|
|
}
|
|
config::store("settings", &settings);
|
|
}
|
|
|
|
// ---- tabs ---------------------------------------------------------------------------
|
|
|
|
fn new_pane(&self, uri: Uri) -> Rc<Pane> {
|
|
let id = self.next_id.get();
|
|
self.next_id.set(id + 1);
|
|
let Some(ui) = self.ui.upgrade() else {
|
|
unreachable!("panes are made while the window lives");
|
|
};
|
|
let pane = Pane::new(id, &ui, self.locations.clone(), self.blocking);
|
|
let me = self.me.clone();
|
|
pane.model.on_changed(move |_| {
|
|
if let Some(shell) = me.upgrade() {
|
|
shell.pane_changed(id);
|
|
}
|
|
});
|
|
if let Some(actions) = self.actions.get() {
|
|
pane.model.set_cut(&actions.cut_uris());
|
|
}
|
|
pane.go(uri);
|
|
pane
|
|
}
|
|
|
|
/// Opens a tab after the last one and makes it current.
|
|
fn open_tab(&self, uris: Vec<Uri>) {
|
|
let panes: Vec<Rc<Pane>> = uris.into_iter().map(|u| self.new_pane(u)).collect();
|
|
let id = self.next_id.get() as i32;
|
|
self.next_id.set(self.next_id.get() + 1);
|
|
let slot = self.tabs.borrow().len();
|
|
let tab = Tab {
|
|
id,
|
|
slot,
|
|
panes,
|
|
active: 0,
|
|
};
|
|
self.tab_rows.push(self.tab_data(&tab));
|
|
self.tabs.borrow_mut().push(tab);
|
|
self.current.set(slot);
|
|
}
|
|
|
|
/// A new tab at the active pane's folder.
|
|
fn new_tab(&self) {
|
|
let dir = self.active().and_then(|p| p.dir());
|
|
self.open_tab(vec![dir.unwrap_or_else(|| Uri::from_path(&places::home()))]);
|
|
self.show_current();
|
|
}
|
|
|
|
fn tab_data(&self, tab: &Tab) -> TabData {
|
|
let dir = tab.panes.get(tab.active).and_then(|p| p.dir());
|
|
let (title, glyph) = dir.map(|d| path::title(&d)).unwrap_or_default();
|
|
TabData {
|
|
id: tab.id,
|
|
title: title.into(),
|
|
glyph: glyph.into(),
|
|
slot: tab.slot as i32,
|
|
}
|
|
}
|
|
|
|
fn index_of(&self, id: i32) -> Option<usize> {
|
|
self.tabs.borrow().iter().position(|t| t.id == id)
|
|
}
|
|
|
|
fn select_tab(&self, index: usize) {
|
|
if index < self.tabs.borrow().len() && index != self.current.get() {
|
|
self.current.set(index);
|
|
self.show_current();
|
|
}
|
|
}
|
|
|
|
fn close_tab(&self, index: usize) {
|
|
let mut tabs = self.tabs.borrow_mut();
|
|
if tabs.len() <= 1 || index >= tabs.len() {
|
|
return;
|
|
}
|
|
let closed = tabs.remove(index);
|
|
for t in tabs.iter_mut().filter(|t| t.slot > closed.slot) {
|
|
t.slot -= 1;
|
|
}
|
|
// The neighbour that takes the closed tab's place, or the one before it.
|
|
let next_slot = closed.slot.min(tabs.len() - 1);
|
|
let next = tabs.iter().position(|t| t.slot == next_slot).unwrap_or(0);
|
|
let was_current = index == self.current.get();
|
|
let current = if was_current {
|
|
next
|
|
} else if self.current.get() > index {
|
|
self.current.get() - 1
|
|
} else {
|
|
self.current.get()
|
|
};
|
|
drop(tabs);
|
|
self.tab_rows.remove(index);
|
|
self.refresh_tab_rows();
|
|
self.current.set(current);
|
|
self.show_current();
|
|
}
|
|
|
|
/// Moves tab `id` to `slot`, shifting the tabs between.
|
|
fn move_tab(&self, id: i32, slot: usize) {
|
|
let mut tabs = self.tabs.borrow_mut();
|
|
let Some(from) = tabs.iter().find(|t| t.id == id).map(|t| t.slot) else {
|
|
return;
|
|
};
|
|
let to = slot.min(tabs.len() - 1);
|
|
for t in tabs.iter_mut() {
|
|
if t.id == id {
|
|
t.slot = to;
|
|
} else if from < t.slot && t.slot <= to {
|
|
t.slot -= 1;
|
|
} else if to <= t.slot && t.slot < from {
|
|
t.slot += 1;
|
|
}
|
|
}
|
|
drop(tabs);
|
|
self.refresh_tab_rows();
|
|
self.show_current();
|
|
}
|
|
|
|
/// Next or previous tab by place, wrapping.
|
|
fn cycle(&self, backward: bool) {
|
|
let tabs = self.tabs.borrow();
|
|
let n = tabs.len();
|
|
let slot = tabs[self.current.get()].slot;
|
|
let want = if backward {
|
|
(slot + n - 1) % n
|
|
} else {
|
|
(slot + 1) % n
|
|
};
|
|
let index = tabs.iter().position(|t| t.slot == want);
|
|
drop(tabs);
|
|
if let Some(index) = index {
|
|
self.select_tab(index);
|
|
}
|
|
}
|
|
|
|
fn refresh_tab_rows(&self) {
|
|
for (i, tab) in self.tabs.borrow().iter().enumerate() {
|
|
let data = self.tab_data(tab);
|
|
if self.tab_rows.row_data(i).as_ref() != Some(&data) {
|
|
self.tab_rows.set_row_data(i, data);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- panes --------------------------------------------------------------------------
|
|
|
|
pub(crate) fn active(&self) -> Option<Rc<Pane>> {
|
|
let tabs = self.tabs.borrow();
|
|
let tab = tabs.get(self.current.get())?;
|
|
tab.panes.get(tab.active).cloned()
|
|
}
|
|
|
|
pub(crate) fn pane_at(&self, index: usize) -> Option<Rc<Pane>> {
|
|
let tabs = self.tabs.borrow();
|
|
tabs.get(self.current.get())?.panes.get(index).cloned()
|
|
}
|
|
|
|
/// Every pane of every tab.
|
|
pub(crate) fn panes(&self) -> Vec<Rc<Pane>> {
|
|
let tabs = self.tabs.borrow();
|
|
tabs.iter().flat_map(|t| t.panes.iter().cloned()).collect()
|
|
}
|
|
|
|
/// Index of the active pane in the current tab.
|
|
pub(crate) fn active_index(&self) -> usize {
|
|
let tabs = self.tabs.borrow();
|
|
tabs.get(self.current.get()).map_or(0, |t| t.active)
|
|
}
|
|
|
|
pub(crate) fn activate_pane(&self, index: usize) {
|
|
self.set_active_pane(index);
|
|
}
|
|
|
|
fn set_active_pane(&self, index: usize) {
|
|
let changed = {
|
|
let mut tabs = self.tabs.borrow_mut();
|
|
let Some(tab) = tabs.get_mut(self.current.get()) else {
|
|
return;
|
|
};
|
|
let ok = index < tab.panes.len() && tab.active != index;
|
|
if ok {
|
|
tab.active = index;
|
|
}
|
|
ok
|
|
};
|
|
if changed {
|
|
self.refresh_tab_rows();
|
|
self.show_chrome();
|
|
}
|
|
}
|
|
|
|
fn toggle_split(&self) {
|
|
let (split, dir) = {
|
|
let tabs = self.tabs.borrow();
|
|
let tab = &tabs[self.current.get()];
|
|
(tab.panes.len() > 1, tab.panes[tab.active].dir())
|
|
};
|
|
if split {
|
|
let mut tabs = self.tabs.borrow_mut();
|
|
let tab = &mut tabs[self.current.get()];
|
|
let keep = tab.panes.remove(tab.active);
|
|
tab.panes = vec![keep];
|
|
tab.active = 0;
|
|
} else {
|
|
let pane = self.new_pane(dir.unwrap_or_else(|| Uri::from_path(&places::home())));
|
|
let mut tabs = self.tabs.borrow_mut();
|
|
let tab = &mut tabs[self.current.get()];
|
|
tab.panes.push(pane);
|
|
tab.active = 1;
|
|
}
|
|
self.show_current();
|
|
}
|
|
|
|
/// A pane's folder model changed: redraw what shows it.
|
|
fn pane_changed(&self, id: u64) {
|
|
let (index, is_active, tab_index) = {
|
|
let tabs = self.tabs.borrow();
|
|
let found = tabs.iter().enumerate().find_map(|(ti, t)| {
|
|
t.panes
|
|
.iter()
|
|
.position(|p| p.id == id)
|
|
.map(|pi| (ti, pi, pi == t.active))
|
|
});
|
|
let Some((ti, pi, active)) = found else {
|
|
return;
|
|
};
|
|
(pi, active, ti)
|
|
};
|
|
if tab_index == self.current.get() {
|
|
if let Some(pane) = self.pane_at(index)
|
|
&& index < self.pane_rows.row_count()
|
|
{
|
|
self.pane_rows.set_row_data(index, pane.data());
|
|
}
|
|
if is_active {
|
|
self.show_chrome();
|
|
}
|
|
}
|
|
if is_active {
|
|
self.refresh_tab_rows();
|
|
}
|
|
}
|
|
|
|
/// Shows the current tab: its panes, then everything that follows the active pane.
|
|
fn show_current(&self) {
|
|
let rows: Vec<PaneData> = {
|
|
let tabs = self.tabs.borrow();
|
|
tabs[self.current.get()]
|
|
.panes
|
|
.iter()
|
|
.map(|p| p.data())
|
|
.collect()
|
|
};
|
|
if rows.len() == self.pane_rows.row_count() {
|
|
for (i, row) in rows.into_iter().enumerate() {
|
|
self.pane_rows.set_row_data(i, row);
|
|
}
|
|
} else {
|
|
self.pane_rows.set_vec(rows);
|
|
}
|
|
self.show_chrome();
|
|
self.focus_active();
|
|
}
|
|
|
|
/// Tab bar selection, toolbar, breadcrumb, sort menu, sidebar and status bar.
|
|
fn show_chrome(&self) {
|
|
let Some(ui) = self.ui.upgrade() else {
|
|
return;
|
|
};
|
|
let (id, slot, active) = {
|
|
let tabs = self.tabs.borrow();
|
|
let t = &tabs[self.current.get()];
|
|
(t.id, t.slot, t.active)
|
|
};
|
|
ui.set_current_tab(id);
|
|
ui.set_current_slot(slot as i32);
|
|
ui.set_active_pane(active as i32);
|
|
let Some(pane) = self.active() else {
|
|
return;
|
|
};
|
|
ui.set_can_back(pane.can_back());
|
|
ui.set_can_forward(pane.can_forward());
|
|
ui.set_can_up(pane.can_up());
|
|
let dir = pane.dir();
|
|
let crumbs: Vec<Crumb> = dir
|
|
.as_ref()
|
|
.map(path::segments)
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.map(|s| Crumb {
|
|
name: s.name.into(),
|
|
glyph: s.glyph.into(),
|
|
})
|
|
.collect();
|
|
ui.set_crumbs(ModelRc::new(VecModel::from(crumbs)));
|
|
ui.set_path(dir.as_ref().map(path::text).unwrap_or_default().into());
|
|
ui.set_sort_menu(ModelRc::new(VecModel::from(pane.model.sort_menu())));
|
|
ui.set_status(status(&pane).into());
|
|
self.show_sidebar(&ui, dir.as_ref());
|
|
}
|
|
|
|
fn show_sidebar(&self, ui: &MainWindow, dir: Option<&Uri>) {
|
|
let places = self.places.borrow();
|
|
let sections: Vec<SidebarSection> = Section::ALL
|
|
.into_iter()
|
|
.map(|section| {
|
|
let items: Vec<PlaceItem> = places
|
|
.items(section)
|
|
.iter()
|
|
.map(|p| PlaceItem {
|
|
name: p.name.as_str().into(),
|
|
glyph: p.glyph.into(),
|
|
current: Some(&p.uri) == dir,
|
|
})
|
|
.collect();
|
|
SidebarSection {
|
|
title: section.title().into(),
|
|
items: ModelRc::new(VecModel::from(items)),
|
|
collapsed: places.collapsed(section),
|
|
empty: section.empty().into(),
|
|
can_add: section == Section::Bookmarks,
|
|
}
|
|
})
|
|
.collect();
|
|
let model = ui.get_sections();
|
|
// Same section count every time: update rows so the collapse animation keeps state.
|
|
match model.as_any().downcast_ref::<VecModel<SidebarSection>>() {
|
|
Some(rows) if rows.row_count() == sections.len() => {
|
|
for (i, s) in sections.into_iter().enumerate() {
|
|
rows.set_row_data(i, s);
|
|
}
|
|
}
|
|
_ => ui.set_sections(ModelRc::new(VecModel::from(sections))),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn focus_active(&self) {
|
|
if let Some(ui) = self.ui.upgrade() {
|
|
ui.set_focus_request(ui.get_focus_request().wrapping_add(1));
|
|
}
|
|
}
|
|
|
|
/// Runs `f` on the active pane, then redraws the chrome (history buttons change).
|
|
fn with_active(&self, f: impl FnOnce(&Pane)) {
|
|
if let Some(pane) = self.active() {
|
|
f(&pane);
|
|
self.show_chrome();
|
|
}
|
|
}
|
|
|
|
fn go(&self, uri: Uri) {
|
|
self.with_active(|p| p.go(uri));
|
|
self.focus_active();
|
|
}
|
|
|
|
// ---- places -------------------------------------------------------------------------
|
|
|
|
fn open_place(&self, section: Section, index: usize, new_tab: bool) {
|
|
let Some(uri) = self
|
|
.places
|
|
.borrow()
|
|
.get(section, index)
|
|
.map(|p| p.uri.clone())
|
|
else {
|
|
return;
|
|
};
|
|
if new_tab {
|
|
self.open_tab(vec![uri]);
|
|
self.show_current();
|
|
} else {
|
|
self.go(uri);
|
|
}
|
|
}
|
|
|
|
fn add_bookmark(&self) {
|
|
let Some(dir) = self.active().and_then(|p| p.dir()) else {
|
|
return;
|
|
};
|
|
let (name, _) = path::title(&dir);
|
|
self.places.borrow_mut().add_bookmark(name, dir);
|
|
self.show_chrome();
|
|
}
|
|
|
|
fn show_place_menu(&self, section: Section, index: usize) {
|
|
let items = self.place_menu(section, index);
|
|
if let Some(ui) = self.ui.upgrade() {
|
|
ui.set_place_menu(ModelRc::new(VecModel::from(items)));
|
|
}
|
|
}
|
|
|
|
fn place_menu(&self, section: Section, index: usize) -> Vec<MenuItem> {
|
|
self.menu_target.set(Some(MenuTarget { section, index }));
|
|
let item = |text: &str, icon: &str, disabled: bool| MenuItem {
|
|
text: text.into(),
|
|
icon: icon.into(),
|
|
shortcut: Default::default(),
|
|
disabled,
|
|
separator: false,
|
|
submenu: ModelRc::new(VecModel::<MenuEntry>::default()),
|
|
};
|
|
let mut items = vec![item("Open in new tab", "\u{e89e}", false)];
|
|
if section == Section::Bookmarks {
|
|
let last = self.places.borrow().items(section).len().saturating_sub(1);
|
|
items.push(item("Rename", "\u{e3c9}", false));
|
|
items.push(item("Move up", "\u{e5d8}", index == 0));
|
|
items.push(item("Move down", "\u{e5db}", index >= last));
|
|
items.push(item("Remove", "\u{e872}", false));
|
|
}
|
|
items
|
|
}
|
|
|
|
fn place_menu_activated(&self, choice: usize) {
|
|
let Some(MenuTarget { section, index }) = self.menu_target.get() else {
|
|
return;
|
|
};
|
|
match choice {
|
|
0 => self.open_place(section, index, true),
|
|
1 => {
|
|
let name = self
|
|
.places
|
|
.borrow()
|
|
.get(section, index)
|
|
.map(|p| p.name.clone());
|
|
if let (Some(name), Some(ui)) = (name, self.ui.upgrade()) {
|
|
ui.invoke_rename_bookmark(name.into());
|
|
}
|
|
return;
|
|
}
|
|
2 => self.places.borrow_mut().move_bookmark(index, -1),
|
|
3 => self.places.borrow_mut().move_bookmark(index, 1),
|
|
4 => self.places.borrow_mut().remove_bookmark(index),
|
|
_ => {}
|
|
}
|
|
self.menu_target.set(None);
|
|
self.show_chrome();
|
|
}
|
|
|
|
fn rename_bookmark(&self, name: &str) {
|
|
if let Some(MenuTarget {
|
|
section: Section::Bookmarks,
|
|
index,
|
|
}) = self.menu_target.take()
|
|
{
|
|
self.places.borrow_mut().rename_bookmark(index, name);
|
|
self.show_chrome();
|
|
}
|
|
}
|
|
|
|
// ---- breadcrumb ---------------------------------------------------------------------
|
|
|
|
/// Completes `text` off the UI thread; the newest request wins.
|
|
fn complete(&self, text: String) {
|
|
let generation = self.completion.fetch_add(1, Ordering::SeqCst) + 1;
|
|
let set = |ui: &MainWindow, items: Vec<(String, String)>| {
|
|
let items: Vec<Completion> = items
|
|
.into_iter()
|
|
.map(|(name, text)| Completion {
|
|
name: name.into(),
|
|
text: text.into(),
|
|
})
|
|
.collect();
|
|
ui.set_completions(ModelRc::new(VecModel::from(items)));
|
|
};
|
|
if text.is_empty() {
|
|
if let Some(ui) = self.ui.upgrade() {
|
|
set(&ui, Vec::new());
|
|
}
|
|
return;
|
|
}
|
|
let current = self.active().and_then(|p| p.dir());
|
|
let locations = self.locations.clone();
|
|
let latest = self.completion.clone();
|
|
let ui = self.ui.clone();
|
|
std::thread::spawn(move || {
|
|
let items = path::complete(&locations, &text, current.as_ref());
|
|
if latest.load(Ordering::SeqCst) != generation {
|
|
return;
|
|
}
|
|
let _ = ui.upgrade_in_event_loop(move |ui| {
|
|
if latest.load(Ordering::SeqCst) == generation {
|
|
set(&ui, items);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
fn path_accepted(&self, text: &str) {
|
|
self.complete(String::new());
|
|
let current = self.active().and_then(|p| p.dir());
|
|
if let Some(uri) = path::parse(text, current.as_ref()) {
|
|
self.go(uri);
|
|
} else {
|
|
self.focus_active();
|
|
}
|
|
}
|
|
|
|
fn go_crumb(&self, index: usize) {
|
|
let target = self
|
|
.active()
|
|
.and_then(|p| p.dir())
|
|
.and_then(|d| path::segments(&d).into_iter().nth(index))
|
|
.map(|s| s.uri);
|
|
if let Some(uri) = target {
|
|
self.go(uri);
|
|
}
|
|
}
|
|
|
|
// ---- wiring -------------------------------------------------------------------------
|
|
|
|
fn bind(&self, ui: &MainWindow) {
|
|
// `on!(setter, |shell, args…| body)`: runs `body` while the shell lives.
|
|
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;
|
|
}
|
|
});
|
|
}};
|
|
}
|
|
// Pane callbacks carry the pane's index in the current tab first.
|
|
macro_rules! on_pane {
|
|
($setter:ident, |$s:ident, $p:ident $(, $arg:ident)*| $body:expr) => {{
|
|
let me = self.me.clone();
|
|
ui.$setter(move |pane $(, $arg)*| {
|
|
if let Some($s) = me.upgrade() {
|
|
if let Some($p) = $s.pane_at(pane.max(0) as usize) {
|
|
$body;
|
|
}
|
|
}
|
|
});
|
|
}};
|
|
}
|
|
let section = |i: i32| Section::ALL.get(usize::try_from(i).ok()?).copied();
|
|
|
|
on!(on_tab_selected, |s, id| if let Some(i) = s.index_of(id) {
|
|
s.select_tab(i)
|
|
});
|
|
on!(on_tab_closed, |s, id| if let Some(i) = s.index_of(id) {
|
|
s.close_tab(i)
|
|
});
|
|
on!(on_tab_moved, |s, id, slot| s
|
|
.move_tab(id, slot.max(0) as usize));
|
|
on!(on_new_tab, |s| s.new_tab());
|
|
on!(on_cycle_tab, |s, backward| s.cycle(backward));
|
|
on!(on_close_tab, |s| s.close_tab(s.current.get()));
|
|
on!(on_pane_activated, |s, i| s
|
|
.set_active_pane(i.max(0) as usize));
|
|
on!(on_toggle_split, |s| s.toggle_split());
|
|
on!(on_toggle_sidebar, |s| if let Some(ui) = s.ui.upgrade() {
|
|
ui.set_show_sidebar(!ui.get_show_sidebar())
|
|
});
|
|
on!(on_back, |s| s.with_active(|p| p.back()));
|
|
on!(on_forward, |s| s.with_active(|p| p.forward()));
|
|
on!(on_up, |s| s.with_active(|p| p.up()));
|
|
on!(on_go_crumb, |s, i| s.go_crumb(i.max(0) as usize));
|
|
on!(on_path_edited, |s, text| s.complete(text.into()));
|
|
on!(on_path_accepted, |s, text| s.path_accepted(&text));
|
|
on!(on_path_reverted, |s| {
|
|
s.complete(String::new());
|
|
s.focus_active()
|
|
});
|
|
|
|
on_pane!(on_sort_by, |_s, p, column| p.model.sort_column(column));
|
|
on_pane!(on_filter_typed, |_s, p, text| p.model.type_filter(&text));
|
|
on_pane!(on_filter_edited, |_s, p, text| p.model.set_filter(&text));
|
|
on_pane!(on_toggle_hidden, |_s, p| p.model.toggle_hidden());
|
|
on_pane!(on_row_clicked, |_s, p, row, ctrl, shift| {
|
|
p.model.click(row.max(0) as usize, ctrl, shift)
|
|
});
|
|
on_pane!(on_row_activated, |s, p, row| {
|
|
p.activate(usize::try_from(row).ok());
|
|
s.show_chrome()
|
|
});
|
|
on_pane!(on_select_all, |_s, p| p.model.select_all());
|
|
on_pane!(on_grid_columns_changed, |_s, p, n| p
|
|
.model
|
|
.set_columns(n.max(1) as usize));
|
|
on_pane!(on_motion_settled, |_s, p| p.model.motion_settled());
|
|
on_pane!(on_view_mode_selected, |_s, p, mode| p.model.set_mode(mode));
|
|
on_pane!(on_clear_selection, |_s, p| p.model.clear_selection());
|
|
let me = self.me.clone();
|
|
ui.on_navigate(move |i, key, shift, page| {
|
|
me.upgrade()
|
|
.and_then(|s| s.pane_at(i.max(0) as usize))
|
|
.map_or(-1, |p| p.model.navigate(key, shift, page.max(1) as usize))
|
|
});
|
|
on!(
|
|
on_sort_menu_activated,
|
|
|s, index, sub| if let Some(p) = s.active() {
|
|
p.model.sort_menu_activated(index.max(0) as usize, sub)
|
|
}
|
|
);
|
|
|
|
on!(
|
|
on_place_opened,
|
|
|s, i, index, new_tab| if let Some(sec) = section(i) {
|
|
s.open_place(sec, index.max(0) as usize, new_tab)
|
|
}
|
|
);
|
|
on!(on_section_toggled, |s, i| if let Some(sec) = section(i) {
|
|
s.places.borrow_mut().toggle(sec);
|
|
s.show_chrome()
|
|
});
|
|
on!(on_add_bookmark, |s| s.add_bookmark());
|
|
on!(
|
|
on_place_menu_requested,
|
|
|s, i, index| if let Some(sec) = section(i) {
|
|
s.show_place_menu(sec, index.max(0) as usize)
|
|
}
|
|
);
|
|
on!(on_place_menu_activated, |s, index, _sub| {
|
|
s.place_menu_activated(index.max(0) as usize)
|
|
});
|
|
on!(on_bookmark_renamed, |s, name| s.rename_bookmark(&name));
|
|
|
|
self.bind_mouse_buttons(ui);
|
|
}
|
|
|
|
/// Mouse back and forward buttons go to the pane under the pointer. Slint's own touch
|
|
/// areas take every button press, so these are caught before Slint sees them.
|
|
fn bind_mouse_buttons(&self, ui: &MainWindow) {
|
|
use slint::winit_030::winit::event::{ElementState, MouseButton, WindowEvent};
|
|
use slint::winit_030::{EventResult, WinitWindowAccessor};
|
|
|
|
let me = self.me.clone();
|
|
let weak = ui.as_weak();
|
|
ui.window().on_winit_window_event(move |_, event| {
|
|
let WindowEvent::MouseInput {
|
|
state: ElementState::Pressed,
|
|
button: button @ (MouseButton::Back | MouseButton::Forward),
|
|
..
|
|
} = event
|
|
else {
|
|
return EventResult::Propagate;
|
|
};
|
|
let (Some(shell), Some(ui)) = (me.upgrade(), weak.upgrade()) else {
|
|
return EventResult::Propagate;
|
|
};
|
|
let hovered = ui.get_hovered_pane();
|
|
if hovered >= 0 {
|
|
shell.set_active_pane(hovered as usize);
|
|
}
|
|
if *button == MouseButton::Back {
|
|
shell.with_active(|p| p.back());
|
|
} else {
|
|
shell.with_active(|p| p.forward());
|
|
}
|
|
EventResult::PreventDefault
|
|
});
|
|
}
|
|
}
|
|
|
|
/// `12 items`, `3 of 12 selected (4.2 MiB)`, plus hidden files left out.
|
|
fn status(pane: &Pane) -> String {
|
|
let state = pane.model.state();
|
|
if state.status == crate::FolderStatus::Loading {
|
|
return "Loading".into();
|
|
}
|
|
if state.status == crate::FolderStatus::Error {
|
|
return String::new();
|
|
}
|
|
let items = state.items;
|
|
let mut text = match pane.model.selection() {
|
|
(0, _) => format!("{items} {}", if items == 1 { "item" } else { "items" }),
|
|
(n, bytes) => format!("{n} of {items} selected ({})", format::size(bytes)),
|
|
};
|
|
if state.hidden > 0 {
|
|
text.push_str(&format!(", {} hidden", state.hidden));
|
|
}
|
|
text
|
|
}
|