Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Aug 29, 2024
1 parent 31314b8 commit c9e8fbb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
50 changes: 32 additions & 18 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{Local, Utc};
use cosmic::app::{command, Core};

use cosmic::iced::advanced::subscription;
Expand All @@ -16,15 +17,15 @@ use cosmic::iced_runtime::command::Action;
use cosmic::iced_runtime::core::window;
use cosmic::iced_style::application;
use cosmic::iced_widget::{qr_code, Column};
use cosmic::widget::{button, icon, text, text_input, MouseArea};
use cosmic::widget::{button, icon, text, text_input, MouseArea, Space};

use cosmic::{Element, Theme};
use futures::executor::block_on;

use crate::config::{Config, CONFIG_VERSION, PRIVATE_MODE};
use crate::db::{self, Db, Entry};
use crate::message::{AppMsg, ConfigMsg};
use crate::navigation::NavigationMessage;
use crate::navigation::EventMsg;
use crate::utils::command_message;
use crate::{clipboard, config, navigation};

Expand All @@ -46,6 +47,7 @@ pub struct AppState {
pub clipboard_state: ClipboardState,
pub focused: usize,
pub qr_code: Option<Result<qr_code::State, ()>>,
last_quit: Option<(i64, PopupKind)>,
}

impl AppState {
Expand Down Expand Up @@ -109,7 +111,9 @@ impl AppState {
self.db.set_query_and_search("".into());

if let Some(popup) = self.popup.take() {
//info!("destroy {:?}", popup.id);
// info!("destroy {:?}", popup.id);

self.last_quit = Some((Utc::now().timestamp_millis(), popup.kind));

if self.config.horizontal {
destroy_layer_surface(popup.id)
Expand All @@ -122,8 +126,17 @@ impl AppState {
}

fn open_popup(&mut self, kind: PopupKind) -> Command<cosmic::app::Message<AppMsg>> {
// handle the case where the popup was closed by clicking the icon
if self
.last_quit
.map(|(t, k)| (Utc::now().timestamp_millis() - t) < 200 && k == kind)
.unwrap_or(false)
{
return Command::none();
}

let new_id = Id::unique();
//info!("will create {:?}", new_id);
// info!("will create {:?}", new_id);

let popup = Popup { kind, id: new_id };
self.popup.replace(popup);
Expand Down Expand Up @@ -204,6 +217,7 @@ impl cosmic::Application for AppState {
focused: 0,
qr_code: None,
config,
last_quit: None,
};

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -305,37 +319,37 @@ impl cosmic::Application for AppState {
self.clipboard_state = ClipboardState::Init;
}
AppMsg::Navigation(message) => match message {
navigation::NavigationMessage::Event(e) => {
navigation::EventMsg::Event(e) => {
let message = match e {
Named::Enter => NavigationMessage::Enter,
Named::Escape => NavigationMessage::Quit,
Named::ArrowDown if !self.config.horizontal => NavigationMessage::Next,
Named::ArrowUp if !self.config.horizontal => NavigationMessage::Previous,
Named::ArrowLeft if self.config.horizontal => NavigationMessage::Previous,
Named::ArrowRight if self.config.horizontal => NavigationMessage::Next,
_ => NavigationMessage::None,
Named::Enter => EventMsg::Enter,
Named::Escape => EventMsg::Quit,
Named::ArrowDown if !self.config.horizontal => EventMsg::Next,
Named::ArrowUp if !self.config.horizontal => EventMsg::Previous,
Named::ArrowLeft if self.config.horizontal => EventMsg::Previous,
Named::ArrowRight if self.config.horizontal => EventMsg::Next,
_ => EventMsg::None,
};

return command_message(AppMsg::Navigation(message));
}
navigation::NavigationMessage::Next => {
navigation::EventMsg::Next => {
self.focus_next();
}
navigation::NavigationMessage::Previous => {
navigation::EventMsg::Previous => {
self.focus_previous();
}
navigation::NavigationMessage::Enter => {
navigation::EventMsg::Enter => {
if let Some(data) = self.db.get(self.focused) {
if let Err(e) = clipboard::copy(data.clone()) {
error!("can't copy: {e}");
}
return self.close_popup();
}
}
navigation::NavigationMessage::Quit => {
navigation::EventMsg::Quit => {
return self.close_popup();
}
NavigationMessage::None => {}
EventMsg::None => {}
},
AppMsg::Db(inner) => {
block_on(async {
Expand Down Expand Up @@ -391,7 +405,7 @@ impl cosmic::Application for AppState {

fn view_window(&self, _id: Id) -> Element<Self::Message> {
let Some(popup) = &self.popup else {
return self.core.applet.popup_container(self.popup_view()).into();
return Space::new(0, 0).into();
};

let view = match &popup.kind {
Expand Down
4 changes: 2 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
clipboard::{self, ClipboardMessage},
config::Config,
db::{self, DbMessage, Entry},
navigation::NavigationMessage,
navigation::EventMsg,
};

#[derive(Clone, Debug)]
Expand All @@ -19,7 +19,7 @@ pub enum AppMsg {
Copy(Entry),
Delete(Entry),
Clear,
Navigation(NavigationMessage),
Navigation(EventMsg),
Db(DbMessage),
ShowQrCode(Entry),
ReturnToClipboard,
Expand Down
20 changes: 16 additions & 4 deletions src/navigation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use cosmic::iced::{event, Subscription};
use cosmic::iced::{
event::{self},
Subscription,
};

#[derive(Debug, Clone)]
pub enum NavigationMessage {
pub enum EventMsg {
Next,
Previous,
Enter,
Expand All @@ -11,12 +14,21 @@ pub enum NavigationMessage {
}

#[allow(clippy::collapsible_match)]
pub fn sub() -> Subscription<NavigationMessage> {
pub fn sub() -> Subscription<EventMsg> {
cosmic::iced_futures::event::listen_with(|event, status| {
match status {
event::Status::Captured => None,
event::Status::Ignored => {
match event {
cosmic::iced::Event::PlatformSpecific(
cosmic::iced::event::PlatformSpecific::Wayland(
cosmic::iced::event::wayland::Event::Layer(
cosmic::iced::event::wayland::LayerEvent::Unfocused,
..,
),
),
) => Some(EventMsg::Quit),

event::Event::Keyboard(event) => match event {
cosmic::iced::keyboard::Event::KeyPressed { key, .. } => {
match key {
Expand All @@ -27,7 +39,7 @@ pub fn sub() -> Subscription<NavigationMessage> {
| cosmic::iced::keyboard::key::Named::ArrowUp
| cosmic::iced::keyboard::key::Named::ArrowLeft
| cosmic::iced::keyboard::key::Named::ArrowRight => {
Some(NavigationMessage::Event(named))
Some(EventMsg::Event(named))
}

/*
Expand Down
13 changes: 5 additions & 8 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ impl AppState {
let btn = cosmic::widget::button(content)
.on_press(AppMsg::Copy(entry.clone()))
.padding([8, 16])
.width(Length::Fill)
.height(Length::Fill)
.style(Button::Custom {
active: Box::new(move |focused, theme| {
let rad_s = theme.cosmic().corner_radii.radius_s;
Expand Down Expand Up @@ -320,13 +318,12 @@ impl AppState {
}),
});

let btn = container(btn);

// XXX: min width
let btn = if self.config.horizontal {
btn.height(Length::Fill).max_width(350f32)
let btn: Element<_> = if self.config.horizontal {
container(btn.width(Length::Fill).height(Length::Fill))
.max_width(350f32)
.into()
} else {
btn.width(Length::Fill)
btn.width(Length::Fill).into()
};

context_menu(
Expand Down

0 comments on commit c9e8fbb

Please sign in to comment.