From 997ab8d09dd009891054c80d5cfea52674b1b3da Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 4 Feb 2024 20:48:27 +0900 Subject: [PATCH] rename APIs which have winit/tao specific names - `UserEvent*` -> `Event*` - `IpcMessage` -> `RendererMessage` --- v2/README.md | 3 ++- v2/src/renderer.rs | 16 ++++++++-------- v2/src/sanity.rs | 6 +++--- v2/src/shiba.rs | 22 +++++++++++----------- v2/src/watcher/mod.rs | 6 +++--- v2/src/watcher/system.rs | 8 ++++---- v2/src/watcher/system_linux.rs | 8 ++++---- v2/src/wry/event_loop.rs | 12 ++++++------ v2/src/wry/webview.rs | 14 +++++++------- 9 files changed, 48 insertions(+), 47 deletions(-) diff --git a/v2/README.md b/v2/README.md index 91132c6..5cb9065 100644 --- a/v2/README.md +++ b/v2/README.md @@ -21,7 +21,8 @@ Features: - Sections outline in side navigation bar. The current section is automatically focused - Both CLI and GUI friendly; Available as a single binary executable as well as a desktop application installed to your system - Performance critical part (parsing Markdown text, searching Markdown AST, calculating the last modified position, ...) and - core application logic are written in [Rust][rust]. View logic is written in [TypeScript][ts] and [React][react] + core application logic are written in [Rust][rust]. View logic written in [TypeScript][ts] and [React][react] runs on + platform-specific WebView - Cross platform; macOS, Windows, Linux are supported - Customizable with [a YAML config file](./src/assets/default_config.yml) (color theme, keyboard shortcuts, custom CSS, ...) - Dogs are respected :dog2: diff --git a/v2/src/renderer.rs b/v2/src/renderer.rs index c79e1c6..985609d 100644 --- a/v2/src/renderer.rs +++ b/v2/src/renderer.rs @@ -70,8 +70,8 @@ pub enum MessageFromRenderer { } #[derive(Debug)] -pub enum UserEvent { - IpcMessage(MessageFromRenderer), +pub enum Event { + RendererMessage(MessageFromRenderer), FileDrop(PathBuf), WatchedFilesChanged(Vec), OpenLocalPath(PathBuf), @@ -154,10 +154,10 @@ pub enum RenderingFlow { Exit, } -/// Sender to send [`UserEvent`] accross threads. It is used to send the user events to the main thread +/// Sender to send [`Event`] accross threads. It is used to send the user events to the main thread /// from another worker thread. -pub trait UserEventSender: 'static + Send { - fn send(&self, event: UserEvent); +pub trait EventSender: 'static + Send { + fn send(&self, event: Event); } /// Renderer is responsible for rendering the actual UI in the rendering context. @@ -185,11 +185,11 @@ pub trait Renderer { /// Context to execute rendering. pub trait Rendering: Sized { - type UserEventSender: UserEventSender; + type EventSender: EventSender; type Renderer: Renderer; fn new() -> Result; - fn create_sender(&self) -> Self::UserEventSender; + fn create_sender(&self) -> Self::EventSender; fn create_renderer(&mut self, config: &Config) -> Result; /// Starts the rendering execution and runs until the process exits. fn start(self, handler: H) -> !; @@ -197,7 +197,7 @@ pub trait Rendering: Sized { /// Event handler which listens several rendering events. pub trait EventHandler { - fn handle_user_event(&mut self, event: UserEvent) -> Result; + fn handle_event(&mut self, event: Event) -> Result; fn handle_menu_event(&mut self, item: MenuItem) -> Result; fn handle_minimized(&mut self, is_active: bool); fn handle_exit(&mut self) -> Result<()>; diff --git a/v2/src/sanity.rs b/v2/src/sanity.rs index 18f4564..86128b9 100644 --- a/v2/src/sanity.rs +++ b/v2/src/sanity.rs @@ -1,4 +1,4 @@ -use crate::renderer::{MessageFromRenderer, UserEvent, UserEventSender}; +use crate::renderer::{Event, EventSender, MessageFromRenderer}; use std::thread::{sleep, spawn}; use std::time::Duration; @@ -6,7 +6,7 @@ pub struct SanityTest { sender: Option, } -impl SanityTest { +impl SanityTest { pub fn new(sender: S) -> Self { Self { sender: Some(sender) } } @@ -39,7 +39,7 @@ impl SanityTest { for msg in messages { sleep(Duration::from_millis(1000)); log::debug!("Sanity test case is about to send message: {msg:?}"); - sender.send(UserEvent::IpcMessage(msg)); + sender.send(Event::RendererMessage(msg)); } }); } diff --git a/v2/src/shiba.rs b/v2/src/shiba.rs index 4da9a5f..dfdc740 100644 --- a/v2/src/shiba.rs +++ b/v2/src/shiba.rs @@ -4,8 +4,8 @@ use crate::dialog::Dialog; use crate::markdown::{DisplayText, MarkdownContent, MarkdownParser}; use crate::opener::Opener; use crate::renderer::{ - EventHandler, MenuItem, MessageFromRenderer, MessageToRenderer, Renderer, Rendering, - RenderingFlow, UserEvent, + Event, EventHandler, MenuItem, MessageFromRenderer, MessageToRenderer, Renderer, Rendering, + RenderingFlow, }; #[cfg(feature = "__sanity")] use crate::sanity::SanityTest; @@ -201,7 +201,7 @@ pub struct Shiba { init_file: Option, _dialog: PhantomData, #[cfg(feature = "__sanity")] - sanity: SanityTest, + sanity: SanityTest, } impl Shiba @@ -368,7 +368,7 @@ where self.opener.open(&path) } - fn handle_ipc_message(&mut self, message: MessageFromRenderer) -> Result { + fn handle_renderer_message(&mut self, message: MessageFromRenderer) -> Result { use MessageFromRenderer::*; match message { Init => { @@ -431,18 +431,18 @@ where W: Watcher, D: Dialog, { - fn handle_user_event(&mut self, event: UserEvent) -> Result { + fn handle_event(&mut self, event: Event) -> Result { log::debug!("Handling user event {:?}", event); match event { - UserEvent::IpcMessage(msg) => return self.handle_ipc_message(msg), - UserEvent::FileDrop(mut path) => { + Event::RendererMessage(msg) => return self.handle_renderer_message(msg), + Event::FileDrop(mut path) => { log::debug!("Previewing file dropped into window: {:?}", path); if !path.is_absolute() { path = path.canonicalize()?; } self.preview_new(path)?; } - UserEvent::WatchedFilesChanged(mut paths) => { + Event::WatchedFilesChanged(mut paths) => { log::debug!("Files changed: {:?}", paths); if let Some(current) = self.history.current() { if paths.contains(current) { @@ -460,7 +460,7 @@ where } } } - UserEvent::OpenLocalPath(mut path) => { + Event::OpenLocalPath(mut path) => { if path.is_relative() { if let Some(current_file) = self.history.current() { if let Some(dir) = current_file.parent() { @@ -478,11 +478,11 @@ where self.opener.open(&path).with_context(|| format!("opening path {:?}", &path))?; } } - UserEvent::OpenExternalLink(link) => { + Event::OpenExternalLink(link) => { log::debug!("Opening external link item clicked in WebView: {:?}", link); self.opener.open(&link).with_context(|| format!("opening link {:?}", &link))?; } - UserEvent::Error(err) => return Err(err), + Event::Error(err) => return Err(err), } Ok(RenderingFlow::Continue) } diff --git a/v2/src/watcher/mod.rs b/v2/src/watcher/mod.rs index 3e2afd3..ea2aaf8 100644 --- a/v2/src/watcher/mod.rs +++ b/v2/src/watcher/mod.rs @@ -9,7 +9,7 @@ pub use system::SystemWatcher; pub use system_linux::SystemWatcher; use crate::config::{FileExtensions, Watch as Config}; -use crate::renderer::UserEventSender; +use crate::renderer::EventSender; use anyhow::Result; use notify::event::{CreateKind, DataChange, EventKind, ModifyKind}; use std::collections::HashMap; @@ -82,14 +82,14 @@ impl PathFilter { } pub trait Watcher: Sized { - fn new(sender: S, filter: PathFilter) -> Result; + fn new(sender: S, filter: PathFilter) -> Result; fn watch(&mut self, path: &Path) -> Result<()>; } pub struct NopWatcher; impl Watcher for NopWatcher { - fn new(_sender: S, _filter: PathFilter) -> Result { + fn new(_sender: S, _filter: PathFilter) -> Result { Ok(Self) } fn watch(&mut self, _path: &Path) -> Result<()> { diff --git a/v2/src/watcher/system.rs b/v2/src/watcher/system.rs index 29fb9ae..81c6313 100644 --- a/v2/src/watcher/system.rs +++ b/v2/src/watcher/system.rs @@ -1,11 +1,11 @@ use super::{find_watch_path_fallback, should_watch_event, PathFilter, Watcher}; -use crate::renderer::{UserEvent, UserEventSender}; +use crate::renderer::{Event, EventSender}; use anyhow::{Context as _, Result}; use notify::{recommended_watcher, RecommendedWatcher, RecursiveMode, Watcher as NotifyWatcher}; use std::path::Path; impl Watcher for RecommendedWatcher { - fn new(sender: S, mut filter: PathFilter) -> Result { + fn new(sender: S, mut filter: PathFilter) -> Result { let watcher = recommended_watcher(move |res: notify::Result| match res { Ok(event) if should_watch_event(event.kind) => { log::debug!("Caught filesystem event: {:?}", event); @@ -15,7 +15,7 @@ impl Watcher for RecommendedWatcher { if !paths.is_empty() { log::debug!("Files change event from watcher: {:?}", paths); - sender.send(UserEvent::WatchedFilesChanged(paths)); + sender.send(Event::WatchedFilesChanged(paths)); } filter.cleanup_debouncer(); @@ -23,7 +23,7 @@ impl Watcher for RecommendedWatcher { Ok(event) => log::debug!("Ignored filesystem event: {:?}", event), Err(err) => { log::error!("Error on watching file changes: {}", err); - sender.send(UserEvent::Error(err.into())); + sender.send(Event::Error(err.into())); } })?; Ok(watcher) diff --git a/v2/src/watcher/system_linux.rs b/v2/src/watcher/system_linux.rs index 6cbe1cb..0fff821 100644 --- a/v2/src/watcher/system_linux.rs +++ b/v2/src/watcher/system_linux.rs @@ -1,5 +1,5 @@ use super::{find_watch_path_fallback, should_watch_event, PathFilter, Watcher}; -use crate::renderer::{UserEvent, UserEventSender}; +use crate::renderer::{Event, EventSender}; use anyhow::{Context as _, Result}; use notify::{recommended_watcher, RecommendedWatcher, RecursiveMode, Watcher as NotifyWatcher}; use std::collections::{HashMap, HashSet}; @@ -66,7 +66,7 @@ pub struct SystemWatcher { } impl Watcher for SystemWatcher { - fn new(sender: S, mut filter: PathFilter) -> Result { + fn new(sender: S, mut filter: PathFilter) -> Result { let watching = Arc::new(Mutex::new(WatchingPaths::default())); let inner = { let watching = watching.clone(); @@ -82,7 +82,7 @@ impl Watcher for SystemWatcher { if !paths.is_empty() { log::debug!("Files change event from watcher: {:?}", paths); - sender.send(UserEvent::WatchedFilesChanged(paths)); + sender.send(Event::WatchedFilesChanged(paths)); } filter.cleanup_debouncer(); @@ -90,7 +90,7 @@ impl Watcher for SystemWatcher { Ok(event) => log::debug!("Ignored filesystem event: {:?}", event), Err(err) => { log::error!("Error on watching file changes: {}", err); - sender.send(UserEvent::Error(err.into())); + sender.send(Event::Error(err.into())); } })? }; diff --git a/v2/src/wry/event_loop.rs b/v2/src/wry/event_loop.rs index 100fa53..a80d83d 100644 --- a/v2/src/wry/event_loop.rs +++ b/v2/src/wry/event_loop.rs @@ -1,5 +1,5 @@ use crate::config::Config; -use crate::renderer::{EventHandler, Rendering, RenderingFlow, UserEvent, UserEventSender}; +use crate::renderer::{Event as AppEvent, EventHandler, EventSender, Rendering, RenderingFlow}; use crate::wry::menu::{Menu, MenuEvents}; use crate::wry::webview::{EventLoop, WebViewRenderer}; use anyhow::Result; @@ -13,8 +13,8 @@ pub struct Wry { menu: Menu, // On Windows, Menu instance needs to be created before creating the event loop } -impl UserEventSender for EventLoopProxy { - fn send(&self, event: UserEvent) { +impl EventSender for EventLoopProxy { + fn send(&self, event: AppEvent) { if let Err(err) = self.send_event(event) { log::error!("Could not send user event for message from WebView: {}", err); } @@ -22,7 +22,7 @@ impl UserEventSender for EventLoopProxy { } impl Rendering for Wry { - type UserEventSender = EventLoopProxy; + type EventSender = EventLoopProxy; type Renderer = WebViewRenderer; #[cfg(not(target_os = "windows"))] @@ -57,7 +57,7 @@ impl Rendering for Wry { Ok(Self { event_loop, menu_events, menu }) } - fn create_sender(&self) -> Self::UserEventSender { + fn create_sender(&self) -> Self::EventSender { self.event_loop.create_proxy() } @@ -84,7 +84,7 @@ impl Rendering for Wry { log::debug!("Closing window was requested"); RenderingFlow::Exit } - Event::UserEvent(event) => handler.handle_user_event(event).unwrap_or_else(|err| { + Event::UserEvent(event) => handler.handle_event(event).unwrap_or_else(|err| { handler.handle_error(err.context("Could not handle user event")) }), Event::WindowEvent { event: WindowEvent::Resized(size), .. } => { diff --git a/v2/src/wry/webview.rs b/v2/src/wry/webview.rs index 53d8495..bbd4e62 100644 --- a/v2/src/wry/webview.rs +++ b/v2/src/wry/webview.rs @@ -2,8 +2,8 @@ use crate::assets::Assets; use crate::config::{Config, WindowTheme as ThemeConfig}; use crate::persistent::WindowState; use crate::renderer::{ - MessageFromRenderer, MessageToRenderer, RawMessageWriter, Renderer, Theme as RendererTheme, - UserEvent, WindowAppearance, ZoomLevel, + Event, MessageFromRenderer, MessageToRenderer, RawMessageWriter, Renderer, + Theme as RendererTheme, WindowAppearance, ZoomLevel, }; use crate::wry::menu::Menu; use anyhow::{Context as _, Result}; @@ -21,7 +21,7 @@ use wry::{FileDropEvent, WebContext, WebView, WebViewBuilder}; #[cfg(target_os = "windows")] use wry::{MemoryUsageLevel, WebViewBuilderExtWindows, WebViewExtWindows}; -pub type EventLoop = tao::event_loop::EventLoop; +pub type EventLoop = tao::event_loop::EventLoop; #[cfg(not(target_os = "macos"))] const ICON_RGBA: &[u8] = include_bytes!("../assets/icon_32x32.rgba"); @@ -57,7 +57,7 @@ fn create_webview(window: &Window, event_loop: &EventLoop, config: &Config) -> R .with_ipc_handler(move |msg| { let msg: MessageFromRenderer = serde_json::from_str(&msg).unwrap(); log::debug!("Message from WebView: {msg:?}"); - if let Err(err) = ipc_proxy.send_event(UserEvent::IpcMessage(msg)) { + if let Err(err) = ipc_proxy.send_event(Event::RendererMessage(msg)) { log::error!("Could not send user event for message from WebView: {err}"); } }) @@ -65,7 +65,7 @@ fn create_webview(window: &Window, event_loop: &EventLoop, config: &Config) -> R if let FileDropEvent::Dropped { paths, .. } = event { log::debug!("Files were dropped (the first one will be opened): {paths:?}",); if let Some(path) = paths.into_iter().next() { - if let Err(err) = file_drop_proxy.send_event(UserEvent::FileDrop(path)) { + if let Err(err) = file_drop_proxy.send_event(Event::FileDrop(path)) { log::error!("Could not send user event for file drop: {err}"); } } @@ -104,10 +104,10 @@ fn create_webview(window: &Window, event_loop: &EventLoop, config: &Config) -> R let path = url.replace('/', "\\").into(); log::debug!("Opening local path {:?}", path); - UserEvent::OpenLocalPath(path) + Event::OpenLocalPath(path) } else { log::debug!("Navigating to external URL {:?}", url); - UserEvent::OpenExternalLink(url) + Event::OpenExternalLink(url) }; if let Err(e) = navigation_proxy.send_event(event) {