mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* fix(settings): merge edits through one persistence owner (t1533) * fix(modes): focus the adopted correction without duplicating it (t1533) * fix(settings): publish reversions even before the host saw the save (t1533)
230 lines
8.4 KiB
Rust
230 lines
8.4 KiB
Rust
//! The Linux window's host: the dictation host in this process
|
|
//! (`linux_app::HostHandle`), which owns the listener, shortcuts, models,
|
|
//! History, and updates. A headless render without one reads the running
|
|
//! Hex's `hex status` instead.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use color_eyre::Result;
|
|
use color_eyre::eyre::eyre;
|
|
|
|
use super::{HostState, ServiceView};
|
|
use crate::app_settings::{AppSettings, HotkeyBinding};
|
|
use crate::desktop::i18n::tr;
|
|
use crate::linux::app::HostHandle;
|
|
pub(super) use crate::linux::app::{HostRequest as Request, ShortcutTarget};
|
|
use crate::linux::input::LinuxHotkey;
|
|
|
|
/// The host registers recorded shortcuts (`SetShortcut`) and saves them.
|
|
pub(super) const HOST_SAVES_SHORTCUTS: bool = true;
|
|
/// Settings records shortcuts from the window's key events.
|
|
pub(super) const RECORDS_KEY_EVENTS: bool = true;
|
|
/// X11 and portal shortcuts paste with Ctrl+V; terminals want Ctrl+Shift+V.
|
|
pub(super) const TERMINAL_PASTE: bool = true;
|
|
/// Linux has no tray, so the window offers Quit Hex.
|
|
pub(super) const QUIT_IN_WINDOW: bool = true;
|
|
|
|
/// Closes an open window and gives the event loop a moment to tear its
|
|
/// surface down before Hex quits. Quitting with the window still open
|
|
/// destroyed it during the loop's exit, which crashed NVIDIA's driver on
|
|
/// Wayland (#821); closing it first, as the window's own close does, exits
|
|
/// cleanly. Bounded, so a loop that never answers still quits.
|
|
pub(super) fn close_before_quit(window: &rune::AppHandle<super::Ui>) {
|
|
use std::time::{Duration, Instant};
|
|
if !window.is_visible() {
|
|
return;
|
|
}
|
|
window.close();
|
|
let deadline = Instant::now() + Duration::from_secs(1);
|
|
while window.is_visible() && Instant::now() < deadline {
|
|
std::thread::sleep(Duration::from_millis(10));
|
|
}
|
|
// The surface goes with the loop's next turns, after the close itself.
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
}
|
|
|
|
/// The host the window's app runs beside; none for a headless `--snap` of
|
|
/// another process's `hex status`.
|
|
pub(crate) type Connect = Option<HostHandle>;
|
|
|
|
pub(super) enum Backend {
|
|
Host(HostHandle, Option<Box<crate::linux::input::MouseCapture>>),
|
|
/// `--snap --scene live` from a separate process.
|
|
Status(Option<Box<crate::linux::service::Status>>, Option<String>),
|
|
}
|
|
|
|
impl Backend {
|
|
pub(super) fn connect(host: Connect, wake: Arc<dyn Fn() + Send + Sync>) -> Self {
|
|
let Some(host) = host else {
|
|
let mut backend = Self::Status(None, None);
|
|
backend.refresh();
|
|
return backend;
|
|
};
|
|
host.set_waker(wake);
|
|
Self::Host(host, None)
|
|
}
|
|
|
|
pub(super) fn send(&mut self, request: Request) -> Result<()> {
|
|
match self {
|
|
Self::Host(host, capture) => {
|
|
match &request {
|
|
Request::CaptureShortcutFor(_)
|
|
if !crate::linux::session::LinuxSession::detect().is_wayland() =>
|
|
{
|
|
*capture = crate::linux::input::MouseCapture::start()
|
|
.map(Box::new)
|
|
.inspect_err(|error| tracing::warn!(%error, "mouse shortcut capture unavailable"))
|
|
.ok();
|
|
}
|
|
Request::CancelShortcut | Request::SetShortcut { .. } => *capture = None,
|
|
_ => {}
|
|
}
|
|
if let Err(error) = host.request(request) {
|
|
*capture = None;
|
|
return Err(error);
|
|
}
|
|
Ok(())
|
|
}
|
|
Self::Status(..) => Err(eyre!("This window only shows `hex status`")),
|
|
}
|
|
}
|
|
|
|
/// Saves through the host, which adopts the settings at once.
|
|
pub(super) fn save(
|
|
&mut self,
|
|
base: &AppSettings,
|
|
settings: &AppSettings,
|
|
) -> Result<AppSettings> {
|
|
match self {
|
|
Self::Host(host, _) => host.save_settings(base, settings),
|
|
Self::Status(..) => Err(eyre!("This window only shows `hex status`")),
|
|
}
|
|
}
|
|
|
|
/// The host drains its work, then asks the app to quit.
|
|
pub(super) fn quit(&mut self) {
|
|
if let Self::Host(host, capture) = self {
|
|
*capture = None;
|
|
host.quit();
|
|
}
|
|
}
|
|
|
|
pub(super) fn closed(&mut self) {
|
|
if let Self::Host(host, capture) = self {
|
|
*capture = None;
|
|
host.window_closed();
|
|
}
|
|
}
|
|
|
|
pub(super) fn refresh(&mut self) {
|
|
match self {
|
|
Self::Host(..) => {}
|
|
Self::Status(status, error) => {
|
|
match crate::linux::service::request(crate::linux::service::Request::Status) {
|
|
Ok(state) => (*status, *error) = (Some(Box::new(state)), None),
|
|
Err(failure) => (*status, *error) = (None, Some(failure.to_string())),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn error(&self) -> Option<String> {
|
|
match self {
|
|
Self::Host(..) => None,
|
|
Self::Status(_, error) => error.clone(),
|
|
}
|
|
}
|
|
|
|
pub(super) fn state(&self) -> Option<HostState> {
|
|
let (desktop, settings, editing) = match self {
|
|
Self::Host(host, _) => {
|
|
let snapshot = host.snapshot();
|
|
(snapshot.desktop, snapshot.settings, snapshot.editing)
|
|
}
|
|
Self::Status(status, _) => {
|
|
let status = *status.clone()?;
|
|
(status.desktop, status.settings, status.editing)
|
|
}
|
|
};
|
|
Some(HostState {
|
|
view: ServiceView {
|
|
listener: desktop.listener,
|
|
operation_error: desktop.operation_error,
|
|
transcription: desktop.transcription,
|
|
update_status: desktop.update_status,
|
|
update_notes: desktop.update_notes,
|
|
update_blocked: None,
|
|
editing,
|
|
},
|
|
settings,
|
|
})
|
|
}
|
|
|
|
/// Keyboard capture remains in Rune; X11 supplies mouse button edges.
|
|
pub(super) fn take_chord(&mut self) -> Option<super::settings::Chord> {
|
|
let Self::Host(_, capture) = self else {
|
|
return None;
|
|
};
|
|
let result = capture.as_mut()?.poll();
|
|
match result {
|
|
Ok(None) => None,
|
|
// Keep listening after a rejected/conflicting candidate. A saved
|
|
// binding ends the grab through SetShortcut above.
|
|
Ok(Some(binding)) => Some(super::settings::Chord::Captured(Ok(HotkeyBinding::from(
|
|
&binding,
|
|
)))),
|
|
Err(error) => {
|
|
tracing::debug!(%error, "mouse shortcut capture ended");
|
|
*capture = None;
|
|
Some(super::settings::Chord::Cancelled)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The host's History store: one store per process.
|
|
pub(super) fn history(
|
|
connect: &Connect,
|
|
_: crate::history::HistoryRetention,
|
|
) -> Option<crate::history::History> {
|
|
connect.as_ref()?.history()
|
|
}
|
|
|
|
/// The window's recorder and `hex settings set` both refuse a key X11 or
|
|
/// the shortcuts portal can't bind, before it is saved.
|
|
pub(super) fn validate_hotkey(
|
|
target: ShortcutTarget,
|
|
binding: &HotkeyBinding,
|
|
) -> Result<(), &'static str> {
|
|
let wayland = crate::linux::session::LinuxSession::detect().is_wayland();
|
|
if binding.key.as_ref().is_some_and(|key| key.is_mouse()) && wayland {
|
|
return Err(tr(
|
|
"Configure mouse shortcuts in your Wayland desktop using the hex commands",
|
|
));
|
|
}
|
|
let hotkey = LinuxHotkey::from(binding);
|
|
hotkey
|
|
.validate()
|
|
// The cancel key is never asked of the portal.
|
|
.and_then(|()| match target {
|
|
ShortcutTarget::Cancel => Ok(()),
|
|
_ if wayland => hotkey.portal_trigger().map(drop),
|
|
_ => Ok(()),
|
|
})
|
|
.map_err(|_| tr("Add a key Linux can register"))
|
|
}
|
|
|
|
/// Wayland never says which application is focused.
|
|
pub(super) fn focused_application_known() -> bool {
|
|
!crate::linux::session::LinuxSession::detect().is_wayland()
|
|
}
|
|
|
|
/// The XDG autostart entry is the source of truth, never `settings.json`.
|
|
pub(super) fn launch_at_login() -> Option<Result<bool, String>> {
|
|
Some(crate::linux::login_item::is_enabled().map_err(|error| format!("{error:#}")))
|
|
}
|
|
|
|
pub(super) fn set_launch_at_login(enabled: bool) -> Result<bool, String> {
|
|
crate::linux::login_item::set_enabled(enabled).map_err(|error| format!("{error:#}"))?;
|
|
crate::linux::login_item::is_enabled().map_err(|error| format!("{error:#}"))
|
|
}
|