Add context menu
This commit is contained in:
parent
2d0a0059d3
commit
ac93fc79d4
7 changed files with 473 additions and 54 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
|
@ -920,7 +920,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-config"
|
name = "cosmic-config"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"atomicwrites",
|
"atomicwrites",
|
||||||
"cosmic-config-derive",
|
"cosmic-config-derive",
|
||||||
|
|
@ -935,7 +935,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-config-derive"
|
name = "cosmic-config-derive"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"syn 1.0.109",
|
"syn 1.0.109",
|
||||||
|
|
@ -984,7 +984,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-theme"
|
name = "cosmic-theme"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"almost",
|
"almost",
|
||||||
"cosmic-config",
|
"cosmic-config",
|
||||||
|
|
@ -2273,7 +2273,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced"
|
name = "iced"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iced_accessibility",
|
"iced_accessibility",
|
||||||
"iced_core",
|
"iced_core",
|
||||||
|
|
@ -2288,7 +2288,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_accessibility"
|
name = "iced_accessibility"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"accesskit",
|
"accesskit",
|
||||||
"accesskit_winit",
|
"accesskit_winit",
|
||||||
|
|
@ -2297,7 +2297,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_core"
|
name = "iced_core"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"instant",
|
"instant",
|
||||||
|
|
@ -2313,7 +2313,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_futures"
|
name = "iced_futures"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"iced_core",
|
"iced_core",
|
||||||
|
|
@ -2326,7 +2326,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_graphics"
|
name = "iced_graphics"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
|
|
@ -2349,7 +2349,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_renderer"
|
name = "iced_renderer"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iced_graphics",
|
"iced_graphics",
|
||||||
"iced_tiny_skia",
|
"iced_tiny_skia",
|
||||||
|
|
@ -2362,7 +2362,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_runtime"
|
name = "iced_runtime"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iced_core",
|
"iced_core",
|
||||||
"iced_futures",
|
"iced_futures",
|
||||||
|
|
@ -2372,7 +2372,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_style"
|
name = "iced_style"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iced_core",
|
"iced_core",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
|
@ -2382,7 +2382,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_tiny_skia"
|
name = "iced_tiny_skia"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
"cosmic-text",
|
"cosmic-text",
|
||||||
|
|
@ -2400,7 +2400,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_wgpu"
|
name = "iced_wgpu"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
|
|
@ -2420,7 +2420,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_widget"
|
name = "iced_widget"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iced_renderer",
|
"iced_renderer",
|
||||||
"iced_runtime",
|
"iced_runtime",
|
||||||
|
|
@ -2434,7 +2434,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iced_winit"
|
name = "iced_winit"
|
||||||
version = "0.12.0"
|
version = "0.12.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iced_graphics",
|
"iced_graphics",
|
||||||
"iced_runtime",
|
"iced_runtime",
|
||||||
|
|
@ -2731,7 +2731,7 @@ checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libcosmic"
|
name = "libcosmic"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/pop-os/libcosmic.git#1bd39b17ae7c97934ea214028cd31c578b589e3e"
|
source = "git+https://github.com/pop-os/libcosmic.git#4674e4b23e80adcebb9096217dd3ff0a2c5ac15b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"apply",
|
"apply",
|
||||||
"ashpd",
|
"ashpd",
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,9 @@ theme = Theme
|
||||||
match-desktop = Match desktop
|
match-desktop = Match desktop
|
||||||
dark = Dark
|
dark = Dark
|
||||||
light = Light
|
light = Light
|
||||||
|
|
||||||
|
# Context menu
|
||||||
|
copy = Copy
|
||||||
|
paste = Paste
|
||||||
|
select-all = Select all
|
||||||
|
new-tab = New tab
|
||||||
|
|
|
||||||
75
src/main.rs
75
src/main.rs
|
|
@ -10,11 +10,15 @@ use cosmic::{
|
||||||
widget::{self, segmented_button},
|
widget::{self, segmented_button},
|
||||||
Application, ApplicationExt, Element,
|
Application, ApplicationExt, Element,
|
||||||
};
|
};
|
||||||
use std::{any::TypeId, env, path::PathBuf, process};
|
use std::{any::TypeId, env, path::PathBuf, process, time::Instant};
|
||||||
|
|
||||||
use config::{AppTheme, Config, CONFIG_VERSION};
|
use config::{AppTheme, Config, CONFIG_VERSION};
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
|
mod menu;
|
||||||
|
|
||||||
|
mod mouse_area;
|
||||||
|
|
||||||
mod localize;
|
mod localize;
|
||||||
|
|
||||||
mod mime_icon;
|
mod mime_icon;
|
||||||
|
|
@ -95,11 +99,9 @@ pub struct Flags {
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
/*TODO
|
|
||||||
Copy,
|
Copy,
|
||||||
Paste,
|
Paste,
|
||||||
SelectAll,
|
SelectAll,
|
||||||
*/
|
|
||||||
Settings,
|
Settings,
|
||||||
TabNew,
|
TabNew,
|
||||||
}
|
}
|
||||||
|
|
@ -107,11 +109,9 @@ pub enum Action {
|
||||||
impl Action {
|
impl Action {
|
||||||
pub fn message(self, entity: segmented_button::Entity) -> Message {
|
pub fn message(self, entity: segmented_button::Entity) -> Message {
|
||||||
match self {
|
match self {
|
||||||
/*TODO
|
|
||||||
Action::Copy => Message::Copy(Some(entity)),
|
Action::Copy => Message::Copy(Some(entity)),
|
||||||
Action::Paste => Message::Paste(Some(entity)),
|
Action::Paste => Message::Paste(Some(entity)),
|
||||||
Action::SelectAll => Message::SelectAll(Some(entity)),
|
Action::SelectAll => Message::SelectAll(Some(entity)),
|
||||||
*/
|
|
||||||
Action::Settings => Message::ToggleContextPage(ContextPage::Settings),
|
Action::Settings => Message::ToggleContextPage(ContextPage::Settings),
|
||||||
Action::TabNew => Message::TabNew,
|
Action::TabNew => Message::TabNew,
|
||||||
}
|
}
|
||||||
|
|
@ -124,6 +124,9 @@ pub enum Message {
|
||||||
Todo,
|
Todo,
|
||||||
AppTheme(AppTheme),
|
AppTheme(AppTheme),
|
||||||
Config(Config),
|
Config(Config),
|
||||||
|
Copy(Option<segmented_button::Entity>),
|
||||||
|
Paste(Option<segmented_button::Entity>),
|
||||||
|
SelectAll(Option<segmented_button::Entity>),
|
||||||
SystemThemeModeChange(cosmic_theme::ThemeMode),
|
SystemThemeModeChange(cosmic_theme::ThemeMode),
|
||||||
TabActivate(segmented_button::Entity),
|
TabActivate(segmented_button::Entity),
|
||||||
TabClose(segmented_button::Entity),
|
TabClose(segmented_button::Entity),
|
||||||
|
|
@ -313,6 +316,23 @@ impl Application for App {
|
||||||
return self.update_config();
|
return self.update_config();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Message::Copy(entity_opt) => {
|
||||||
|
log::warn!("TODO: COPY");
|
||||||
|
}
|
||||||
|
Message::Paste(entity_opt) => {
|
||||||
|
log::warn!("TODO: PASTE");
|
||||||
|
}
|
||||||
|
Message::SelectAll(entity_opt) => {
|
||||||
|
let entity = entity_opt.unwrap_or_else(|| self.tab_model.active());
|
||||||
|
if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
|
||||||
|
if let Some(ref mut items) = tab.items_opt {
|
||||||
|
let select_time = Instant::now();
|
||||||
|
for item in items.iter_mut() {
|
||||||
|
item.select_time = Some(select_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Message::SystemThemeModeChange(_theme_mode) => {
|
Message::SystemThemeModeChange(_theme_mode) => {
|
||||||
return self.update_config();
|
return self.update_config();
|
||||||
}
|
}
|
||||||
|
|
@ -479,32 +499,29 @@ impl Application for App {
|
||||||
let entity = self.tab_model.active();
|
let entity = self.tab_model.active();
|
||||||
match self.tab_model.data::<Tab>(entity) {
|
match self.tab_model.data::<Tab>(entity) {
|
||||||
Some(tab) => {
|
Some(tab) => {
|
||||||
tab_column = tab_column.push(
|
let mut mouse_area = mouse_area::MouseArea::new(
|
||||||
tab.view(self.core())
|
tab.view(self.core())
|
||||||
.map(move |message| Message::TabMessage(entity, message)),
|
.map(move |message| Message::TabMessage(entity, message)),
|
||||||
);
|
)
|
||||||
|
.on_press(move |_point_opt| Message::TabMessage(entity, tab::Message::Click(None)));
|
||||||
/*TODO
|
if tab.context_menu.is_some() {
|
||||||
let terminal_box = terminal_box(terminal).on_context_menu(move |position_opt| {
|
mouse_area = mouse_area
|
||||||
Message::TabContextMenu(entity, position_opt)
|
.on_right_press(move |_point_opt| Message::TabContextMenu(entity, None));
|
||||||
});
|
} else {
|
||||||
|
mouse_area = mouse_area.on_right_press(move |point_opt| {
|
||||||
let context_menu = {
|
Message::TabContextMenu(entity, point_opt)
|
||||||
let terminal = terminal.lock().unwrap();
|
});
|
||||||
terminal.context_menu
|
}
|
||||||
};
|
let mut popover = widget::popover(mouse_area, menu::context_menu(entity));
|
||||||
|
match tab.context_menu {
|
||||||
let tab_element: Element<'_, Message> = match tab.context_menu {
|
Some(point) => {
|
||||||
Some(position) => widget::popover(
|
popover = popover.position(point);
|
||||||
terminal_box.context_menu(position),
|
}
|
||||||
menu::context_menu(&self.config, entity),
|
None => {
|
||||||
)
|
popover = popover.show_popup(false);
|
||||||
.position(position)
|
}
|
||||||
.into(),
|
}
|
||||||
None => terminal_box.into(),
|
tab_column = tab_column.push(popover);
|
||||||
};
|
|
||||||
tab_column = tab_column.push(tab_element);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
//TODO
|
//TODO
|
||||||
|
|
|
||||||
60
src/menu.rs
Normal file
60
src/menu.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
use cosmic::{
|
||||||
|
//TODO: export in cosmic::widget
|
||||||
|
iced::{
|
||||||
|
widget::{column, horizontal_rule},
|
||||||
|
Alignment, Background, Length,
|
||||||
|
},
|
||||||
|
theme,
|
||||||
|
widget::{self, segmented_button},
|
||||||
|
Element,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{fl, Action, Config, Message};
|
||||||
|
|
||||||
|
macro_rules! menu_button {
|
||||||
|
($($x:expr),+ $(,)?) => (
|
||||||
|
widget::button(
|
||||||
|
widget::Row::with_children(
|
||||||
|
vec![$(Element::from($x)),+]
|
||||||
|
)
|
||||||
|
.align_items(Alignment::Center)
|
||||||
|
)
|
||||||
|
.height(Length::Fixed(32.0))
|
||||||
|
.padding([4, 16])
|
||||||
|
.width(Length::Fill)
|
||||||
|
.style(theme::Button::MenuItem)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn context_menu<'a>(entity: segmented_button::Entity) -> Element<'a, Message> {
|
||||||
|
let menu_action = |label, action| {
|
||||||
|
menu_button!(widget::text(label)).on_press(Message::TabContextAction(entity, action))
|
||||||
|
};
|
||||||
|
|
||||||
|
widget::container(column!(
|
||||||
|
menu_action(fl!("copy"), Action::Copy),
|
||||||
|
menu_action(fl!("paste"), Action::Paste),
|
||||||
|
menu_action(fl!("select-all"), Action::SelectAll),
|
||||||
|
horizontal_rule(1),
|
||||||
|
menu_action(fl!("new-tab"), Action::TabNew),
|
||||||
|
menu_action(fl!("settings"), Action::Settings),
|
||||||
|
))
|
||||||
|
.padding(1)
|
||||||
|
//TODO: move style to libcosmic
|
||||||
|
.style(theme::Container::custom(|theme| {
|
||||||
|
let cosmic = theme.cosmic();
|
||||||
|
let component = &cosmic.background.component;
|
||||||
|
widget::container::Appearance {
|
||||||
|
icon_color: Some(component.on.into()),
|
||||||
|
text_color: Some(component.on.into()),
|
||||||
|
background: Some(Background::Color(component.base.into())),
|
||||||
|
border_radius: 8.0.into(),
|
||||||
|
border_width: 1.0,
|
||||||
|
border_color: component.divider.into(),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.width(Length::Fixed(240.0))
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ pub const FALLBACK_MIME_ICON: &str = "text-x-generic";
|
||||||
|
|
||||||
#[derive(Debug, Eq, Hash, PartialEq)]
|
#[derive(Debug, Eq, Hash, PartialEq)]
|
||||||
struct MimeIconKey {
|
struct MimeIconKey {
|
||||||
|
//TODO: this stores icon data for every path, instead store per mime type
|
||||||
path: String,
|
path: String,
|
||||||
size: u16,
|
size: u16,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
330
src/mouse_area.rs
Normal file
330
src/mouse_area.rs
Normal file
|
|
@ -0,0 +1,330 @@
|
||||||
|
//! A container for capturing mouse events.
|
||||||
|
|
||||||
|
use cosmic::iced_core::{
|
||||||
|
event::{self, Event},
|
||||||
|
layout, mouse, overlay, renderer, touch,
|
||||||
|
widget::{tree, Operation, OperationOutputWrapper, Tree},
|
||||||
|
{Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Widget},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Emit messages on mouse events.
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct MouseArea<'a, Message, Renderer> {
|
||||||
|
content: Element<'a, Message, Renderer>,
|
||||||
|
on_drag: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
on_press: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
on_release: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
on_right_press: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
on_right_release: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
on_middle_press: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
on_middle_release: Option<Box<dyn Fn(Option<Point>) -> Message + 'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
|
||||||
|
/// The message to emit when a drag is initiated.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_drag(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_drag = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The message to emit on a left button press.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_press(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_press = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The message to emit on a left button release.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_release(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_release = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The message to emit on a right button press.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_right_press(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_right_press = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The message to emit on a right button release.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_right_release(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_right_release = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The message to emit on a middle button press.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_middle_press(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_middle_press = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The message to emit on a middle button release.
|
||||||
|
#[must_use]
|
||||||
|
pub fn on_middle_release(mut self, message: impl Fn(Option<Point>) -> Message + 'a) -> Self {
|
||||||
|
self.on_middle_release = Some(Box::new(message));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Local state of the [`MouseArea`].
|
||||||
|
#[derive(Default)]
|
||||||
|
struct State {
|
||||||
|
// TODO: Support on_mouse_enter and on_mouse_exit
|
||||||
|
drag_initiated: Option<Point>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
|
||||||
|
/// Creates a [`MouseArea`] with the given content.
|
||||||
|
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
|
||||||
|
MouseArea {
|
||||||
|
content: content.into(),
|
||||||
|
on_drag: None,
|
||||||
|
on_press: None,
|
||||||
|
on_release: None,
|
||||||
|
on_right_press: None,
|
||||||
|
on_right_release: None,
|
||||||
|
on_middle_press: None,
|
||||||
|
on_middle_release: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> Widget<Message, Renderer> for MouseArea<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: renderer::Renderer,
|
||||||
|
Message: Clone,
|
||||||
|
{
|
||||||
|
fn tag(&self) -> tree::Tag {
|
||||||
|
tree::Tag::of::<State>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn state(&self) -> tree::State {
|
||||||
|
tree::State::new(State::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn children(&self) -> Vec<Tree> {
|
||||||
|
vec![Tree::new(&self.content)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn diff(&mut self, tree: &mut Tree) {
|
||||||
|
tree.diff_children(std::slice::from_mut(&mut self.content));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn width(&self) -> Length {
|
||||||
|
self.content.as_widget().width()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn height(&self) -> Length {
|
||||||
|
self.content.as_widget().height()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout(
|
||||||
|
&self,
|
||||||
|
tree: &mut Tree,
|
||||||
|
renderer: &Renderer,
|
||||||
|
limits: &layout::Limits,
|
||||||
|
) -> layout::Node {
|
||||||
|
self.content
|
||||||
|
.as_widget()
|
||||||
|
.layout(&mut tree.children[0], renderer, limits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(
|
||||||
|
&self,
|
||||||
|
tree: &mut Tree,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
renderer: &Renderer,
|
||||||
|
operation: &mut dyn Operation<OperationOutputWrapper<Message>>,
|
||||||
|
) {
|
||||||
|
self.content
|
||||||
|
.as_widget()
|
||||||
|
.operate(&mut tree.children[0], layout, renderer, operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_event(
|
||||||
|
&mut self,
|
||||||
|
tree: &mut Tree,
|
||||||
|
event: Event,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor: mouse::Cursor,
|
||||||
|
renderer: &Renderer,
|
||||||
|
clipboard: &mut dyn Clipboard,
|
||||||
|
shell: &mut Shell<'_, Message>,
|
||||||
|
viewport: &Rectangle,
|
||||||
|
) -> event::Status {
|
||||||
|
if let event::Status::Captured = self.content.as_widget_mut().on_event(
|
||||||
|
&mut tree.children[0],
|
||||||
|
event.clone(),
|
||||||
|
layout,
|
||||||
|
cursor,
|
||||||
|
renderer,
|
||||||
|
clipboard,
|
||||||
|
shell,
|
||||||
|
viewport,
|
||||||
|
) {
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
|
||||||
|
update(
|
||||||
|
self,
|
||||||
|
&event,
|
||||||
|
layout,
|
||||||
|
cursor,
|
||||||
|
shell,
|
||||||
|
tree.state.downcast_mut::<State>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mouse_interaction(
|
||||||
|
&self,
|
||||||
|
tree: &Tree,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor: mouse::Cursor,
|
||||||
|
viewport: &Rectangle,
|
||||||
|
renderer: &Renderer,
|
||||||
|
) -> mouse::Interaction {
|
||||||
|
self.content.as_widget().mouse_interaction(
|
||||||
|
&tree.children[0],
|
||||||
|
layout,
|
||||||
|
cursor,
|
||||||
|
viewport,
|
||||||
|
renderer,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(
|
||||||
|
&self,
|
||||||
|
tree: &Tree,
|
||||||
|
renderer: &mut Renderer,
|
||||||
|
theme: &Renderer::Theme,
|
||||||
|
renderer_style: &renderer::Style,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor: mouse::Cursor,
|
||||||
|
viewport: &Rectangle,
|
||||||
|
) {
|
||||||
|
self.content.as_widget().draw(
|
||||||
|
&tree.children[0],
|
||||||
|
renderer,
|
||||||
|
theme,
|
||||||
|
renderer_style,
|
||||||
|
layout,
|
||||||
|
cursor,
|
||||||
|
viewport,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn overlay<'b>(
|
||||||
|
&'b mut self,
|
||||||
|
tree: &'b mut Tree,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
renderer: &Renderer,
|
||||||
|
) -> Option<overlay::Element<'b, Message, Renderer>> {
|
||||||
|
self.content
|
||||||
|
.as_widget_mut()
|
||||||
|
.overlay(&mut tree.children[0], layout, renderer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> From<MouseArea<'a, Message, Renderer>>
|
||||||
|
for Element<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Message: 'a + Clone,
|
||||||
|
Renderer: 'a + renderer::Renderer,
|
||||||
|
{
|
||||||
|
fn from(area: MouseArea<'a, Message, Renderer>) -> Element<'a, Message, Renderer> {
|
||||||
|
Element::new(area)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Processes the given [`Event`] and updates the [`State`] of an [`MouseArea`]
|
||||||
|
/// accordingly.
|
||||||
|
fn update<Message: Clone, Renderer>(
|
||||||
|
widget: &mut MouseArea<'_, Message, Renderer>,
|
||||||
|
event: &Event,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor: mouse::Cursor,
|
||||||
|
shell: &mut Shell<'_, Message>,
|
||||||
|
state: &mut State,
|
||||||
|
) -> event::Status {
|
||||||
|
if !cursor.is_over(layout.bounds()) {
|
||||||
|
return event::Status::Ignored;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(message) = widget.on_press.as_ref() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
|
| Event::Touch(touch::Event::FingerPressed { .. }) = event
|
||||||
|
{
|
||||||
|
state.drag_initiated = cursor.position();
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(message) = widget.on_release.as_ref() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
||||||
|
| Event::Touch(touch::Event::FingerLifted { .. }) = event
|
||||||
|
{
|
||||||
|
state.drag_initiated = None;
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(message) = widget.on_right_press.as_ref() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) = event {
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(message) = widget.on_right_release.as_ref() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Right)) = event {
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(message) = widget.on_middle_press.as_ref() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Middle)) = event {
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(message) = widget.on_middle_release.as_ref() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Middle)) = event {
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.drag_initiated.is_none() && widget.on_drag.is_some() {
|
||||||
|
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
|
| Event::Touch(touch::Event::FingerPressed { .. }) = event
|
||||||
|
{
|
||||||
|
state.drag_initiated = cursor.position();
|
||||||
|
}
|
||||||
|
} else if let Some((message, drag_source)) = widget.on_drag.as_ref().zip(state.drag_initiated) {
|
||||||
|
if let Some(position) = cursor.position() {
|
||||||
|
if position.distance(drag_source) > 1.0 {
|
||||||
|
state.drag_initiated = None;
|
||||||
|
shell.publish(message(cursor.position_in(layout.bounds())));
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
event::Status::Ignored
|
||||||
|
}
|
||||||
23
src/tab.rs
23
src/tab.rs
|
|
@ -208,7 +208,7 @@ pub fn rescan(tab_path: PathBuf) -> Vec<Item> {
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
Click(usize),
|
Click(Option<usize>),
|
||||||
Home,
|
Home,
|
||||||
Parent,
|
Parent,
|
||||||
}
|
}
|
||||||
|
|
@ -276,10 +276,10 @@ impl Tab {
|
||||||
pub fn update(&mut self, message: Message) -> bool {
|
pub fn update(&mut self, message: Message) -> bool {
|
||||||
let mut cd = None;
|
let mut cd = None;
|
||||||
match message {
|
match message {
|
||||||
Message::Click(click_i) => {
|
Message::Click(click_i_opt) => {
|
||||||
if let Some(ref mut items) = self.items_opt {
|
if let Some(ref mut items) = self.items_opt {
|
||||||
for (i, item) in items.iter_mut().enumerate() {
|
for (i, item) in items.iter_mut().enumerate() {
|
||||||
if i == click_i {
|
if Some(i) == click_i_opt {
|
||||||
if let Some(select_time) = item.select_time {
|
if let Some(select_time) = item.select_time {
|
||||||
if select_time.elapsed() < DOUBLE_CLICK_DURATION {
|
if select_time.elapsed() < DOUBLE_CLICK_DURATION {
|
||||||
if item.is_dir {
|
if item.is_dir {
|
||||||
|
|
@ -306,6 +306,7 @@ impl Tab {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.context_menu = None;
|
||||||
}
|
}
|
||||||
Message::Home => {
|
Message::Home => {
|
||||||
cd = Some(crate::home_dir());
|
cd = Some(crate::home_dir());
|
||||||
|
|
@ -380,7 +381,7 @@ impl Tab {
|
||||||
.width(Length::Fixed(128.0)),
|
.width(Length::Fixed(128.0)),
|
||||||
)
|
)
|
||||||
.style(button_style(item.select_time.is_some()))
|
.style(button_style(item.select_time.is_some()))
|
||||||
.on_press(Message::Click(i))
|
.on_press(Message::Click(Some(i)))
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
count += 1;
|
count += 1;
|
||||||
|
|
@ -420,7 +421,7 @@ impl Tab {
|
||||||
)
|
)
|
||||||
.style(button_style(item.select_time.is_some()))
|
.style(button_style(item.select_time.is_some()))
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.on_press(Message::Click(i))
|
.on_press(Message::Click(Some(i)))
|
||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
count += 1;
|
count += 1;
|
||||||
|
|
@ -436,10 +437,14 @@ impl Tab {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn view(&self, core: &Core) -> Element<Message> {
|
pub fn view(&self, core: &Core) -> Element<Message> {
|
||||||
widget::scrollable(match self.view {
|
widget::container(
|
||||||
View::Grid => self.grid_view(core),
|
widget::scrollable(match self.view {
|
||||||
View::List => self.list_view(core),
|
View::Grid => self.grid_view(core),
|
||||||
})
|
View::List => self.list_view(core),
|
||||||
|
})
|
||||||
|
.width(Length::Fill),
|
||||||
|
)
|
||||||
|
.height(Length::Fill)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue