Drop cookie import; click-through notifications; per-app zoom
The cookie import is gone. It worked mechanically - 43 cookies decrypted from Arc and verifiably visible to the page - but Google, Microsoft and Odoo all refused the imported sessions, because each binds a session to the browser that created it. Signing in once inside the app is simpler and actually works, so the whole path is deleted rather than kept as a feature that mostly fails. That takes rusqlite, aes, cbc, pbkdf2, hmac, sha1 and sha2 out of the build with it. Notifications are now raised through mac-notification-sys rather than Tauri's notification plugin, because the plugin cannot report that one was clicked. A click switches to the app that raised it and then runs the page's own click handler - the only thing that knows which message the notification was about. Zoom is per app, on a fixed ladder so Cmd+0 returns to exactly 100%. The shortcuts are menu-bar accelerators rather than a key listener, since the keystroke has to work while a remote page has focus. The hidden-element count is off the nav rows.
This commit is contained in:
+109
-117
@@ -4,7 +4,7 @@
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use tauri::{AppHandle, Emitter, Manager, State};
|
||||
use url::Url;
|
||||
|
||||
use crate::config::{self, App, Config, Group};
|
||||
@@ -51,15 +51,6 @@ impl AppState {
|
||||
}
|
||||
self.persist()
|
||||
}
|
||||
|
||||
/// Every host the import is allowed to bring cookies across for: the
|
||||
/// configured apps, plus the sign-in hosts that vouch for them.
|
||||
fn importable_hosts(&self) -> Vec<String> {
|
||||
let cfg = self.config.lock().unwrap();
|
||||
let mut hosts: Vec<String> = cfg.apps.iter().flat_map(|a| a.scopes()).collect();
|
||||
hosts.extend(crate::routing::identity_providers().iter().map(|s| s.to_string()));
|
||||
hosts
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_state(handle: &AppHandle) -> Result<AppState, String> {
|
||||
@@ -243,6 +234,7 @@ pub fn add_app(
|
||||
group_id,
|
||||
user_agent: None,
|
||||
hidden: Vec::new(),
|
||||
zoom: 1.0,
|
||||
order,
|
||||
};
|
||||
cfg.apps.push(new.clone());
|
||||
@@ -501,116 +493,106 @@ pub fn test_notification(app_id: String, app: AppHandle) -> Result<(), String> {
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Asks the page which cookies it can actually see.
|
||||
// ----------------------------------------------------------------- zoom
|
||||
|
||||
/// The zoom ladder, so a keystroke lands on a sensible size rather than
|
||||
/// drifting by a multiplier that never returns to exactly 100%.
|
||||
const ZOOM_STEPS: [f64; 13] = [
|
||||
0.5, 0.67, 0.75, 0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0,
|
||||
];
|
||||
|
||||
fn step_zoom(current: f64, direction: i32) -> f64 {
|
||||
// The nearest rung, so a hand-edited value still moves somewhere sane.
|
||||
let idx = ZOOM_STEPS
|
||||
.iter()
|
||||
.enumerate()
|
||||
.min_by(|a, b| {
|
||||
(a.1 - current).abs().partial_cmp(&(b.1 - current).abs()).unwrap()
|
||||
})
|
||||
.map(|(i, _)| i as i32)
|
||||
.unwrap_or(5);
|
||||
let next = (idx + direction).clamp(0, ZOOM_STEPS.len() as i32 - 1);
|
||||
ZOOM_STEPS[next as usize]
|
||||
}
|
||||
|
||||
/// Applies a zoom change to whichever app is showing, and remembers it.
|
||||
///
|
||||
/// `setCookie` is fire-and-forget, so the import's count is what was handed to
|
||||
/// WebKit, not what WebKit kept. This reads the other end. HttpOnly cookies are
|
||||
/// invisible to script by design, so the answer is a floor, not a total — but a
|
||||
/// zero here means the injection never landed at all.
|
||||
/// Per app, because a dense ERP and a mail client do not want the same size.
|
||||
pub fn adjust_zoom(app: &AppHandle, direction: Option<i32>) {
|
||||
let state = app.state::<AppState>();
|
||||
let Some(id) = state.active.lock().unwrap().clone() else { return };
|
||||
|
||||
let zoom = {
|
||||
let mut cfg = state.config.lock().unwrap();
|
||||
let Some(target) = cfg.apps.iter_mut().find(|a| a.id == id) else { return };
|
||||
target.zoom = match direction {
|
||||
Some(d) => step_zoom(target.zoom, d),
|
||||
None => 1.0,
|
||||
};
|
||||
target.zoom
|
||||
};
|
||||
let _ = state.persist();
|
||||
|
||||
if let Some(wv) = app.get_webview(&webviews::label_for(&id)) {
|
||||
let _ = wv.set_zoom(zoom);
|
||||
}
|
||||
let _ = app.emit("zoom-changed", (id, zoom));
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn probe_cookies(app_id: String, app: AppHandle) -> Result<(), String> {
|
||||
pub fn set_zoom(app_id: String, zoom: f64, app: AppHandle, state: State<'_, AppState>) -> Result<Config, String> {
|
||||
{
|
||||
let mut cfg = state.config.lock().unwrap();
|
||||
let target = cfg
|
||||
.apps
|
||||
.iter_mut()
|
||||
.find(|a| a.id == app_id)
|
||||
.ok_or_else(|| format!("no app {app_id}"))?;
|
||||
target.zoom = zoom.clamp(0.25, 5.0);
|
||||
}
|
||||
state.persist()?;
|
||||
if let Some(wv) = app.get_webview(&webviews::label_for(&app_id)) {
|
||||
let _ = wv.set_zoom(zoom);
|
||||
}
|
||||
Ok(state.cfg())
|
||||
}
|
||||
|
||||
/// Reloads whichever app is showing, for the menu bar's Reload item.
|
||||
pub fn reload_active(app: &AppHandle) {
|
||||
let state = app.state::<AppState>();
|
||||
let Some(id) = state.active.lock().unwrap().clone() else { return };
|
||||
if let Some(wv) = app.get_webview(&webviews::label_for(&id)) {
|
||||
let _ = wv.eval("location.reload()");
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------- notification clicks
|
||||
|
||||
/// Runs the page's own click handler for a notification it raised.
|
||||
///
|
||||
/// This is what makes a click land on the message rather than merely on the
|
||||
/// app: Gmail's handler knows which thread it was about, and this app does not
|
||||
/// and should not.
|
||||
#[tauri::command]
|
||||
pub fn notification_click(app_id: String, notification_id: String, app: AppHandle) -> Result<(), String> {
|
||||
let wv = app
|
||||
.get_webview(&webviews::label_for(&app_id))
|
||||
.ok_or_else(|| format!("{app_id} has no webview"))?;
|
||||
wv.eval(
|
||||
r#"(function () {
|
||||
var names = document.cookie
|
||||
? document.cookie.split(';').map(function (c) { return c.split('=')[0].trim(); })
|
||||
: [];
|
||||
if (window.__workAppSend) {
|
||||
window.__workAppSend('diag', {
|
||||
host: location.hostname,
|
||||
visible: String(names.length),
|
||||
names: names.slice(0, 12).join(',')
|
||||
});
|
||||
}
|
||||
})();"#,
|
||||
)
|
||||
let escaped = notification_id.replace('\\', "\\\\").replace('\'', "\\'");
|
||||
wv.eval(&format!(
|
||||
"window.__workAppNotifyClick && window.__workAppNotifyClick('{escaped}')"
|
||||
))
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// What the last cookie probe saw.
|
||||
/// Brings the window forward when a notification is clicked.
|
||||
#[tauri::command]
|
||||
pub fn cookie_probe(app: AppHandle) -> String {
|
||||
let diag = app.state::<AppState>().diag.lock().unwrap().clone();
|
||||
if diag.is_empty() { "no answer from the page".into() } else { diag }
|
||||
}
|
||||
|
||||
// ------------------------------------------------------- browser pairing
|
||||
|
||||
#[tauri::command]
|
||||
pub fn list_browsers() -> Vec<crate::cookies::Browser> {
|
||||
crate::cookies::list()
|
||||
}
|
||||
|
||||
/// Imports the paired browser's cookies for the configured hosts.
|
||||
///
|
||||
/// Everything outside those hosts is dropped before anything is written: this
|
||||
/// reaches into a browser's whole cookie store, and it must come back with the
|
||||
/// sessions for the tools on the list and nothing else.
|
||||
#[tauri::command]
|
||||
pub fn pair_browser(
|
||||
browser: String,
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<crate::cookies::PairResult, String> {
|
||||
let all = crate::cookies::read_all(&browser)?;
|
||||
let scanned = all.len();
|
||||
let wanted = crate::cookies::filter_to_scopes(all, &state.importable_hosts());
|
||||
|
||||
let mut domains: Vec<String> = wanted.iter().map(|c| c.domain.clone()).collect();
|
||||
domains.sort();
|
||||
domains.dedup();
|
||||
|
||||
let mut warnings = Vec::new();
|
||||
if wanted.is_empty() {
|
||||
warnings.push(format!(
|
||||
"Read {scanned} cookies, none for the apps on your list. \
|
||||
Sign in to them in that browser first."
|
||||
));
|
||||
pub fn focus_window(app: AppHandle) {
|
||||
if let Some(w) = app.get_window("main") {
|
||||
let _ = w.unminimize();
|
||||
let _ = w.show();
|
||||
let _ = w.set_focus();
|
||||
}
|
||||
|
||||
let imported = crate::cookies::inject::install(&app, wanted)?;
|
||||
|
||||
{
|
||||
let mut cfg = state.config.lock().unwrap();
|
||||
cfg.settings.paired_browser = Some(browser);
|
||||
cfg.settings.last_paired_at = Some(now_iso());
|
||||
}
|
||||
state.persist()?;
|
||||
|
||||
Ok(crate::cookies::PairResult {
|
||||
imported,
|
||||
domains: domains.len(),
|
||||
domain_names: domains,
|
||||
warnings,
|
||||
})
|
||||
}
|
||||
|
||||
/// A timestamp for "last paired", without pulling in a date library for it.
|
||||
fn now_iso() -> String {
|
||||
let secs = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0);
|
||||
let days = secs / 86_400;
|
||||
let (h, m) = ((secs % 86_400) / 3600, (secs % 3600) / 60);
|
||||
let (y, mo, d) = civil_from_days(days as i64);
|
||||
format!("{y:04}-{mo:02}-{d:02} {h:02}:{m:02} UTC")
|
||||
}
|
||||
|
||||
/// Howard Hinnant's days-to-civil-date algorithm.
|
||||
fn civil_from_days(z: i64) -> (i64, u32, u32) {
|
||||
let z = z + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z.rem_euclid(146_097);
|
||||
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
|
||||
let y = yoe + era * 400;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
|
||||
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
|
||||
(if m <= 2 { y + 1 } else { y }, m, d)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -618,12 +600,22 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn civil_from_days_matches_known_dates() {
|
||||
// Cross-checked against Python:
|
||||
// date(1970,1,1) + timedelta(days=n)
|
||||
assert_eq!(civil_from_days(0), (1970, 1, 1));
|
||||
assert_eq!(civil_from_days(19_723), (2024, 1, 1));
|
||||
assert_eq!(civil_from_days(20_697), (2026, 9, 1));
|
||||
assert_eq!(civil_from_days(20_698), (2026, 9, 2));
|
||||
fn zoom_steps_up_and_down_the_ladder() {
|
||||
assert_eq!(step_zoom(1.0, 1), 1.1);
|
||||
assert_eq!(step_zoom(1.0, -1), 0.9);
|
||||
assert_eq!(step_zoom(1.25, 1), 1.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_stops_at_the_ends_rather_than_wrapping() {
|
||||
assert_eq!(step_zoom(3.0, 1), 3.0);
|
||||
assert_eq!(step_zoom(0.5, -1), 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_off_ladder_value_snaps_to_its_nearest_rung() {
|
||||
// A hand-edited apps.json should still zoom somewhere sensible.
|
||||
assert_eq!(step_zoom(1.04, 1), 1.1);
|
||||
assert_eq!(step_zoom(1.04, -1), 0.9);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user