feat: create additional empty windows at runtime
This commit is contained in:
611
src/main.rs
611
src/main.rs
@@ -17,7 +17,9 @@ use std::sync::{Arc, Mutex};
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tao::dpi::{LogicalPosition, LogicalSize};
|
use tao::dpi::{LogicalPosition, LogicalSize};
|
||||||
use tao::event::{Event as TaoEvent, WindowEvent};
|
use tao::event::{Event as TaoEvent, WindowEvent};
|
||||||
use tao::event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy};
|
use tao::event_loop::{
|
||||||
|
ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget,
|
||||||
|
};
|
||||||
use tao::window::{Theme, Window, WindowBuilder, WindowId};
|
use tao::window::{Theme, Window, WindowBuilder, WindowId};
|
||||||
use wry::{WebContext, WebView, WebViewBuilder};
|
use wry::{WebContext, WebView, WebViewBuilder};
|
||||||
|
|
||||||
@@ -35,6 +37,7 @@ struct SelfWriteRecord {
|
|||||||
enum UserEvent {
|
enum UserEvent {
|
||||||
NewFile(WindowId),
|
NewFile(WindowId),
|
||||||
OpenFile(WindowId),
|
OpenFile(WindowId),
|
||||||
|
NewWindow(WindowId),
|
||||||
OpenPaths(Vec<PathBuf>, bool),
|
OpenPaths(Vec<PathBuf>, bool),
|
||||||
ActivateTab(WindowId, u64),
|
ActivateTab(WindowId, u64),
|
||||||
CloseTab(WindowId, u64),
|
CloseTab(WindowId, u64),
|
||||||
@@ -68,6 +71,7 @@ struct WindowState {
|
|||||||
warned_external_change: Option<PathBuf>,
|
warned_external_change: Option<PathBuf>,
|
||||||
pending_external_change: Option<PathBuf>,
|
pending_external_change: Option<PathBuf>,
|
||||||
dirty: bool,
|
dirty: bool,
|
||||||
|
is_primary: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowState {
|
impl WindowState {
|
||||||
@@ -97,7 +101,7 @@ struct AppState {
|
|||||||
active_window: WindowId,
|
active_window: WindowId,
|
||||||
recent_files: Arc<Mutex<Vec<PathBuf>>>,
|
recent_files: Arc<Mutex<Vec<PathBuf>>>,
|
||||||
strings: Strings,
|
strings: Strings,
|
||||||
_web_context: WebContext,
|
web_context: WebContext,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
@@ -3635,6 +3639,12 @@ fn persist_session(session: &DocumentSession) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn persist_window_session(ws: &WindowState) {
|
||||||
|
if ws.is_primary {
|
||||||
|
persist_session(&ws.session.lock().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn update_tabs(webview: &WebView, session: &DocumentSession) {
|
fn update_tabs(webview: &WebView, session: &DocumentSession) {
|
||||||
let state = tabs_json(session);
|
let state = tabs_json(session);
|
||||||
let _ = webview.evaluate_script(&format!("if(window.__setTabs)window.__setTabs({state});"));
|
let _ = webview.evaluate_script(&format!("if(window.__setTabs)window.__setTabs({state});"));
|
||||||
@@ -3766,6 +3776,320 @@ fn build_startup_page(strings: &Strings) -> String {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_webview(
|
||||||
|
window: &Window,
|
||||||
|
web_context: &mut WebContext,
|
||||||
|
window_id: WindowId,
|
||||||
|
session: Arc<Mutex<DocumentSession>>,
|
||||||
|
last_self_write: Arc<Mutex<Option<SelfWriteRecord>>>,
|
||||||
|
recent_files: Arc<Mutex<Vec<PathBuf>>>,
|
||||||
|
proxy: EventLoopProxy<UserEvent>,
|
||||||
|
initial_page: String,
|
||||||
|
) -> Result<WebView, String> {
|
||||||
|
let proxy_for_ipc = proxy.clone();
|
||||||
|
let proxy_for_navigation = proxy.clone();
|
||||||
|
let proxy_for_drag = proxy.clone();
|
||||||
|
let builder = WebViewBuilder::with_web_context(web_context)
|
||||||
|
.with_html(&initial_page)
|
||||||
|
.with_navigation_handler(move |url: String| {
|
||||||
|
// Let wry load the initial in-memory document; route any real URL click
|
||||||
|
// to the app's tab model or the system default handler.
|
||||||
|
if url.starts_with("http://")
|
||||||
|
|| url.starts_with("https://")
|
||||||
|
|| url.starts_with("mailto:")
|
||||||
|
{
|
||||||
|
let _ = open::that(&url);
|
||||||
|
false
|
||||||
|
} else if let Some(path) = local_document_path_from_url(&url) {
|
||||||
|
let _ = proxy_for_navigation.send_event(UserEvent::OpenPaths(vec![path], false));
|
||||||
|
false
|
||||||
|
} else if url.starts_with("file:") {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_ipc_handler(move |msg| {
|
||||||
|
let body = msg.body();
|
||||||
|
if body == "new-file" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::NewFile(window_id));
|
||||||
|
} else if body == "open" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::OpenFile(window_id));
|
||||||
|
} else if body == "new-window" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::NewWindow(window_id));
|
||||||
|
} else if let Some(index) = body.strip_prefix("open-recent:") {
|
||||||
|
if let Ok(index) = index.parse::<usize>() {
|
||||||
|
let path = recent_files.lock().unwrap().get(index).cloned();
|
||||||
|
if let Some(path) = path {
|
||||||
|
if path.exists() {
|
||||||
|
let _ =
|
||||||
|
proxy_for_ipc.send_event(UserEvent::OpenPaths(vec![path], false));
|
||||||
|
} else if forget_recent_file(&recent_files, &path) {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::RecentChanged);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Some(url) = body.strip_prefix("open-local-link:") {
|
||||||
|
let active_path = session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||||
|
if let Some(path) = resolve_document_link(url, active_path.as_deref()) {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::OpenPaths(vec![path], false));
|
||||||
|
}
|
||||||
|
} else if let Some(rest) = body.strip_prefix("tab-action:") {
|
||||||
|
let (header, pending_content) = rest
|
||||||
|
.split_once('\n')
|
||||||
|
.map(|(header, content)| (header, Some(content)))
|
||||||
|
.unwrap_or((rest, None));
|
||||||
|
let mut parts = header.splitn(2, ':');
|
||||||
|
let action = parts.next().unwrap_or("");
|
||||||
|
let id = parts.next().and_then(|value| value.parse::<u64>().ok());
|
||||||
|
let Some(id) = id else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if let Some(content) = pending_content {
|
||||||
|
let path = session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||||
|
let Some(path) = path else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
*last_self_write.lock().unwrap() = Some(SelfWriteRecord {
|
||||||
|
path: path.clone(),
|
||||||
|
content: content.to_string(),
|
||||||
|
});
|
||||||
|
match fs::write(&path, content) {
|
||||||
|
Ok(()) => {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(window_id, path));
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
|
||||||
|
window_id,
|
||||||
|
path.clone(),
|
||||||
|
error.to_string(),
|
||||||
|
));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match action {
|
||||||
|
"activate" => {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::ActivateTab(window_id, id));
|
||||||
|
}
|
||||||
|
"close" => {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::CloseTab(window_id, id));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
} else if let Some(id) = body.strip_prefix("locate-tab:") {
|
||||||
|
if let Ok(id) = id.parse::<u64>() {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::LocateTab(window_id, id));
|
||||||
|
}
|
||||||
|
} else if body == "dirty:1" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(window_id, true));
|
||||||
|
} else if body == "dirty:0" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(window_id, false));
|
||||||
|
} else if body == "external-change:dirty" {
|
||||||
|
let _ =
|
||||||
|
proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(window_id, true));
|
||||||
|
} else if body == "external-change:clean" {
|
||||||
|
let _ =
|
||||||
|
proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(window_id, false));
|
||||||
|
} else if body == "print" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::Print(window_id));
|
||||||
|
} else if body == "ready" {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::Ready(window_id));
|
||||||
|
} else if let Some(percent) = body.strip_prefix("zoom:") {
|
||||||
|
if let Ok(percent) = percent.parse::<u32>() {
|
||||||
|
save_zoom(percent);
|
||||||
|
}
|
||||||
|
} else if body == "refresh" {
|
||||||
|
if let Some(path) = session.lock().unwrap().active().map(|tab| tab.path.clone()) {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::FileChanged(window_id, path));
|
||||||
|
}
|
||||||
|
} else if let Some(content) = body.strip_prefix("save:") {
|
||||||
|
let path = session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||||
|
if let Some(path) = path {
|
||||||
|
*last_self_write.lock().unwrap() = Some(SelfWriteRecord {
|
||||||
|
path: path.clone(),
|
||||||
|
content: content.to_string(),
|
||||||
|
});
|
||||||
|
match fs::write(&path, content) {
|
||||||
|
Ok(()) => {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(window_id, path));
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
|
||||||
|
window_id,
|
||||||
|
path.clone(),
|
||||||
|
error.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_drag_drop_handler(move |event| {
|
||||||
|
if let wry::DragDropEvent::Drop { paths, .. } = event {
|
||||||
|
let paths = paths
|
||||||
|
.into_iter()
|
||||||
|
.filter(|path| is_supported_document(path))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
if !paths.is_empty() {
|
||||||
|
let _ = proxy_for_drag.send_event(UserEvent::OpenPaths(paths, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wry supplies its own browser options, which can mask WebView2's
|
||||||
|
// environment override. Honor explicit diagnostic arguments only when set.
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
let builder = {
|
||||||
|
use wry::WebViewBuilderExtWindows;
|
||||||
|
match std::env::var("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS") {
|
||||||
|
Ok(arguments) => builder.with_additional_browser_args(&arguments),
|
||||||
|
Err(_) => builder,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
let webview = {
|
||||||
|
use tao::platform::unix::WindowExtUnix;
|
||||||
|
use wry::WebViewBuilderExtUnix;
|
||||||
|
|
||||||
|
let vbox = window
|
||||||
|
.default_vbox()
|
||||||
|
.expect("failed to get default GTK container");
|
||||||
|
builder.build_gtk(vbox)
|
||||||
|
};
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
let webview = builder.build(window);
|
||||||
|
|
||||||
|
webview.map_err(|error| error.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_WINDOWS: usize = 16;
|
||||||
|
|
||||||
|
fn secondary_geom(source: &WindowState, target: &EventLoopWindowTarget<UserEvent>) -> WindowGeom {
|
||||||
|
let scale = source.window.scale_factor();
|
||||||
|
let size = source.window.inner_size();
|
||||||
|
let w = (size.width as f64 / scale).max(400.0);
|
||||||
|
let h = (size.height as f64 / scale).max(300.0);
|
||||||
|
let (mut x, mut y) = source
|
||||||
|
.window
|
||||||
|
.outer_position()
|
||||||
|
.map(|pos| (pos.x as f64 / scale + 28.0, pos.y as f64 / scale + 28.0))
|
||||||
|
.unwrap_or((140.0, 140.0));
|
||||||
|
if let Some(monitor) = target.available_monitors().next() {
|
||||||
|
let ms = monitor.scale_factor();
|
||||||
|
let mp = monitor.position();
|
||||||
|
let msize = monitor.size();
|
||||||
|
let mx = mp.x as f64 / ms;
|
||||||
|
let my = mp.y as f64 / ms;
|
||||||
|
let mw = msize.width as f64 / ms;
|
||||||
|
let mh = msize.height as f64 / ms;
|
||||||
|
x = x.clamp(mx, (mx + mw - w).max(mx));
|
||||||
|
y = y.clamp(my, (my + mh - h).max(my));
|
||||||
|
}
|
||||||
|
WindowGeom { x, y, w, h }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_empty_window(
|
||||||
|
target: &EventLoopWindowTarget<UserEvent>,
|
||||||
|
app: &mut AppState,
|
||||||
|
proxy: &EventLoopProxy<UserEvent>,
|
||||||
|
) -> Result<WindowId, String> {
|
||||||
|
if app.windows.len() >= MAX_WINDOWS {
|
||||||
|
return Err(format!(
|
||||||
|
"At most {MAX_WINDOWS} windows can be open at once."
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let source = app
|
||||||
|
.windows
|
||||||
|
.get(&app.active_window)
|
||||||
|
.ok_or_else(|| "No active window.".to_string())?;
|
||||||
|
let geom = secondary_geom(source, target);
|
||||||
|
let theme = load_theme_choice();
|
||||||
|
let mut builder = WindowBuilder::new()
|
||||||
|
.with_title("MD Preview")
|
||||||
|
.with_inner_size(LogicalSize::new(geom.w, geom.h))
|
||||||
|
.with_position(LogicalPosition::new(geom.x, geom.y))
|
||||||
|
.with_theme(theme.tao_theme());
|
||||||
|
if let Some(icon) = load_window_icon() {
|
||||||
|
builder = builder.with_window_icon(Some(icon));
|
||||||
|
}
|
||||||
|
let window = builder.build(target).map_err(|error| error.to_string())?;
|
||||||
|
let window_id = window.id();
|
||||||
|
let page = build_startup_page(&app.strings);
|
||||||
|
let session = Arc::new(Mutex::new(DocumentSession::default()));
|
||||||
|
let last_self_write = Arc::new(Mutex::new(None));
|
||||||
|
let recent_files = Arc::clone(&app.recent_files);
|
||||||
|
let webview = build_webview(
|
||||||
|
&window,
|
||||||
|
&mut app.web_context,
|
||||||
|
window_id,
|
||||||
|
Arc::clone(&session),
|
||||||
|
Arc::clone(&last_self_write),
|
||||||
|
recent_files,
|
||||||
|
proxy.clone(),
|
||||||
|
page,
|
||||||
|
)?;
|
||||||
|
let enhance_flags = Arc::new(Mutex::new(EnhanceFlags::default()));
|
||||||
|
let watcher = Arc::new(Mutex::new(None));
|
||||||
|
app.windows.insert(
|
||||||
|
window_id,
|
||||||
|
WindowState {
|
||||||
|
webview,
|
||||||
|
window,
|
||||||
|
session,
|
||||||
|
last_self_write,
|
||||||
|
watcher,
|
||||||
|
enhance_flags,
|
||||||
|
loaded_enhancers: EnhanceFlags::default(),
|
||||||
|
page_ready: false,
|
||||||
|
pending_launches: Vec::new(),
|
||||||
|
pending_close: false,
|
||||||
|
warned_external_change: None,
|
||||||
|
pending_external_change: None,
|
||||||
|
dirty: false,
|
||||||
|
is_primary: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
app.active_window = window_id;
|
||||||
|
if let Some(ws) = app.windows.get(&window_id) {
|
||||||
|
ws.window.set_focus();
|
||||||
|
}
|
||||||
|
Ok(window_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close_window(app: &mut AppState, window_id: WindowId, control_flow: &mut ControlFlow) {
|
||||||
|
let was_primary = app
|
||||||
|
.windows
|
||||||
|
.get(&window_id)
|
||||||
|
.map(|ws| ws.is_primary)
|
||||||
|
.unwrap_or(false);
|
||||||
|
if was_primary {
|
||||||
|
if let Some(ws) = app.windows.get(&window_id) {
|
||||||
|
save_window_geom(&ws.window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
app.windows.remove(&window_id);
|
||||||
|
if app.windows.is_empty() {
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if was_primary {
|
||||||
|
if let Some((_, ws)) = app.windows.iter_mut().next() {
|
||||||
|
ws.is_primary = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if app.active_window == window_id {
|
||||||
|
if let Some(next) = app.windows.keys().next().copied() {
|
||||||
|
app.active_window = next;
|
||||||
|
if let Some(ws) = app.windows.get(&next) {
|
||||||
|
ws.window.set_focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
apply_linux_webkit_compat_env();
|
apply_linux_webkit_compat_env();
|
||||||
|
|
||||||
@@ -3884,10 +4208,6 @@ fn main() {
|
|||||||
let document_session = Arc::new(Mutex::new(initial_session));
|
let document_session = Arc::new(Mutex::new(initial_session));
|
||||||
let enhance_flags: Arc<Mutex<EnhanceFlags>> = Arc::new(Mutex::new(initial_flags));
|
let enhance_flags: Arc<Mutex<EnhanceFlags>> = Arc::new(Mutex::new(initial_flags));
|
||||||
let last_self_write: Arc<Mutex<Option<SelfWriteRecord>>> = Arc::new(Mutex::new(None));
|
let last_self_write: Arc<Mutex<Option<SelfWriteRecord>>> = Arc::new(Mutex::new(None));
|
||||||
let session_for_ipc = Arc::clone(&document_session);
|
|
||||||
let recent_files_for_ipc = Arc::clone(&recent_files);
|
|
||||||
let last_self_write_for_ipc = Arc::clone(&last_self_write);
|
|
||||||
let proxy_for_ipc = proxy.clone();
|
|
||||||
|
|
||||||
// Windows: steer WebView2's cache/cookie tree into %LOCALAPPDATA% instead of
|
// Windows: steer WebView2's cache/cookie tree into %LOCALAPPDATA% instead of
|
||||||
// letting it drop next to the exe. Other platforms: use default (None).
|
// letting it drop next to the exe. Other platforms: use default (None).
|
||||||
@@ -3905,201 +4225,20 @@ fn main() {
|
|||||||
};
|
};
|
||||||
let mut web_context = wry::WebContext::new(data_dir);
|
let mut web_context = wry::WebContext::new(data_dir);
|
||||||
|
|
||||||
let proxy_for_navigation = proxy.clone();
|
let webview = match build_webview(
|
||||||
let builder = WebViewBuilder::with_web_context(&mut web_context)
|
&window,
|
||||||
.with_html(&initial_page)
|
&mut web_context,
|
||||||
.with_navigation_handler(move |url: String| {
|
window_id,
|
||||||
// Let wry load the initial in-memory document; route any real URL click
|
Arc::clone(&document_session),
|
||||||
// to the app's tab model or the system default handler.
|
Arc::clone(&last_self_write),
|
||||||
if url.starts_with("http://")
|
Arc::clone(&recent_files),
|
||||||
|| url.starts_with("https://")
|
proxy.clone(),
|
||||||
|| url.starts_with("mailto:")
|
initial_page,
|
||||||
{
|
) {
|
||||||
let _ = open::that(&url);
|
|
||||||
false
|
|
||||||
} else if let Some(path) = local_document_path_from_url(&url) {
|
|
||||||
let _ = proxy_for_navigation.send_event(UserEvent::OpenPaths(vec![path], false));
|
|
||||||
false
|
|
||||||
} else if url.starts_with("file:") {
|
|
||||||
false
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.with_ipc_handler(move |msg| {
|
|
||||||
let body = msg.body();
|
|
||||||
if body == "new-file" {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::NewFile(window_id));
|
|
||||||
} else if body == "open" {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::OpenFile(window_id));
|
|
||||||
} else if let Some(index) = body.strip_prefix("open-recent:") {
|
|
||||||
if let Ok(index) = index.parse::<usize>() {
|
|
||||||
let path = recent_files_for_ipc.lock().unwrap().get(index).cloned();
|
|
||||||
if let Some(path) = path {
|
|
||||||
if path.exists() {
|
|
||||||
let _ =
|
|
||||||
proxy_for_ipc.send_event(UserEvent::OpenPaths(vec![path], false));
|
|
||||||
} else if forget_recent_file(&recent_files_for_ipc, &path) {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::RecentChanged);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if let Some(url) = body.strip_prefix("open-local-link:") {
|
|
||||||
let active_path = session_for_ipc
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.active()
|
|
||||||
.map(|tab| tab.path.clone());
|
|
||||||
if let Some(path) = resolve_document_link(url, active_path.as_deref()) {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::OpenPaths(vec![path], false));
|
|
||||||
}
|
|
||||||
} else if let Some(rest) = body.strip_prefix("tab-action:") {
|
|
||||||
let (header, pending_content) = rest
|
|
||||||
.split_once('\n')
|
|
||||||
.map(|(header, content)| (header, Some(content)))
|
|
||||||
.unwrap_or((rest, None));
|
|
||||||
let mut parts = header.splitn(2, ':');
|
|
||||||
let action = parts.next().unwrap_or("");
|
|
||||||
let id = parts.next().and_then(|value| value.parse::<u64>().ok());
|
|
||||||
let Some(id) = id else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
if let Some(content) = pending_content {
|
|
||||||
let path = session_for_ipc
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.active()
|
|
||||||
.map(|tab| tab.path.clone());
|
|
||||||
let Some(path) = path else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
*last_self_write_for_ipc.lock().unwrap() = Some(SelfWriteRecord {
|
|
||||||
path: path.clone(),
|
|
||||||
content: content.to_string(),
|
|
||||||
});
|
|
||||||
match fs::write(&path, content) {
|
|
||||||
Ok(()) => {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(window_id, path));
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
|
|
||||||
window_id,
|
|
||||||
path.clone(),
|
|
||||||
error.to_string(),
|
|
||||||
));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match action {
|
|
||||||
"activate" => {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::ActivateTab(window_id, id));
|
|
||||||
}
|
|
||||||
"close" => {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::CloseTab(window_id, id));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
} else if let Some(id) = body.strip_prefix("locate-tab:") {
|
|
||||||
if let Ok(id) = id.parse::<u64>() {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::LocateTab(window_id, id));
|
|
||||||
}
|
|
||||||
} else if body == "dirty:1" {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(window_id, true));
|
|
||||||
} else if body == "dirty:0" {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(window_id, false));
|
|
||||||
} else if body == "external-change:dirty" {
|
|
||||||
let _ =
|
|
||||||
proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(window_id, true));
|
|
||||||
} else if body == "external-change:clean" {
|
|
||||||
let _ =
|
|
||||||
proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(window_id, false));
|
|
||||||
} else if body == "print" {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::Print(window_id));
|
|
||||||
} else if body == "ready" {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::Ready(window_id));
|
|
||||||
} else if let Some(percent) = body.strip_prefix("zoom:") {
|
|
||||||
if let Ok(percent) = percent.parse::<u32>() {
|
|
||||||
save_zoom(percent);
|
|
||||||
}
|
|
||||||
} else if body == "refresh" {
|
|
||||||
if let Some(path) = session_for_ipc
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.active()
|
|
||||||
.map(|tab| tab.path.clone())
|
|
||||||
{
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::FileChanged(window_id, path));
|
|
||||||
}
|
|
||||||
} else if let Some(content) = body.strip_prefix("save:") {
|
|
||||||
let path = session_for_ipc
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.active()
|
|
||||||
.map(|tab| tab.path.clone());
|
|
||||||
if let Some(path) = path {
|
|
||||||
*last_self_write_for_ipc.lock().unwrap() = Some(SelfWriteRecord {
|
|
||||||
path: path.clone(),
|
|
||||||
content: content.to_string(),
|
|
||||||
});
|
|
||||||
match fs::write(&path, content) {
|
|
||||||
Ok(()) => {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(window_id, path));
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
|
|
||||||
window_id,
|
|
||||||
path.clone(),
|
|
||||||
error.to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.with_drag_drop_handler({
|
|
||||||
let proxy = proxy.clone();
|
|
||||||
move |event| {
|
|
||||||
if let wry::DragDropEvent::Drop { paths, .. } = event {
|
|
||||||
let paths = paths
|
|
||||||
.into_iter()
|
|
||||||
.filter(|path| is_supported_document(path))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
if !paths.is_empty() {
|
|
||||||
let _ = proxy.send_event(UserEvent::OpenPaths(paths, false));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Wry supplies its own browser options, which can mask WebView2's
|
|
||||||
// environment override. Honor explicit diagnostic arguments only when set.
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
let builder = {
|
|
||||||
use wry::WebViewBuilderExtWindows;
|
|
||||||
match std::env::var("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS") {
|
|
||||||
Ok(arguments) => builder.with_additional_browser_args(&arguments),
|
|
||||||
Err(_) => builder,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
let webview = {
|
|
||||||
use tao::platform::unix::WindowExtUnix;
|
|
||||||
use wry::WebViewBuilderExtUnix;
|
|
||||||
|
|
||||||
let vbox = window
|
|
||||||
.default_vbox()
|
|
||||||
.expect("failed to get default GTK container");
|
|
||||||
builder.build_gtk(vbox).expect("failed to build webview")
|
|
||||||
};
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
|
||||||
let webview = match builder.build(&window) {
|
|
||||||
Ok(webview) => webview,
|
Ok(webview) => webview,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!("Could not initialize WebView: {error}");
|
eprintln!("Could not initialize WebView: {error}");
|
||||||
show_warning_dialog("Could Not Open MD Preview", &error.to_string());
|
show_warning_dialog("Could Not Open MD Preview", &error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -4140,15 +4279,16 @@ fn main() {
|
|||||||
warned_external_change: None,
|
warned_external_change: None,
|
||||||
pending_external_change: None,
|
pending_external_change: None,
|
||||||
dirty: initial_dirty,
|
dirty: initial_dirty,
|
||||||
|
is_primary: true,
|
||||||
},
|
},
|
||||||
)]),
|
)]),
|
||||||
active_window: window_id,
|
active_window: window_id,
|
||||||
recent_files: Arc::clone(&recent_files),
|
recent_files: Arc::clone(&recent_files),
|
||||||
strings,
|
strings,
|
||||||
_web_context: web_context,
|
web_context,
|
||||||
};
|
};
|
||||||
|
|
||||||
event_loop.run(move |event, _target, control_flow| {
|
event_loop.run(move |event, target, control_flow| {
|
||||||
*control_flow = ControlFlow::Wait;
|
*control_flow = ControlFlow::Wait;
|
||||||
let strings = app.strings.clone();
|
let strings = app.strings.clone();
|
||||||
let recent_files = Arc::clone(&app.recent_files);
|
let recent_files = Arc::clone(&app.recent_files);
|
||||||
@@ -4195,6 +4335,14 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
TaoEvent::UserEvent(UserEvent::NewWindow(window_id)) => {
|
||||||
|
if !app.windows.contains_key(&window_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if let Err(error) = create_empty_window(target, &mut app, &proxy) {
|
||||||
|
show_warning_dialog("Could Not Create Window", &error);
|
||||||
|
}
|
||||||
|
}
|
||||||
TaoEvent::UserEvent(UserEvent::OpenFile(window_id)) => {
|
TaoEvent::UserEvent(UserEvent::OpenFile(window_id)) => {
|
||||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||||
return;
|
return;
|
||||||
@@ -4243,7 +4391,9 @@ fn main() {
|
|||||||
session.activate(id);
|
session.activate(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
persist_session(&session);
|
if ws.is_primary {
|
||||||
|
persist_session(&session);
|
||||||
|
}
|
||||||
if preserve_active {
|
if preserve_active {
|
||||||
update_tabs(&ws.webview, &session);
|
update_tabs(&ws.webview, &session);
|
||||||
} else {
|
} else {
|
||||||
@@ -4259,7 +4409,9 @@ fn main() {
|
|||||||
};
|
};
|
||||||
let mut session = ws.session.lock().unwrap();
|
let mut session = ws.session.lock().unwrap();
|
||||||
if session.activate(id) {
|
if session.activate(id) {
|
||||||
persist_session(&session);
|
if ws.is_primary {
|
||||||
|
persist_session(&session);
|
||||||
|
}
|
||||||
drop(session);
|
drop(session);
|
||||||
ws.render_active(&recent_files, &strings);
|
ws.render_active(&recent_files, &strings);
|
||||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||||
@@ -4273,7 +4425,9 @@ fn main() {
|
|||||||
let mut session = ws.session.lock().unwrap();
|
let mut session = ws.session.lock().unwrap();
|
||||||
let was_active = session.active_id == Some(id);
|
let was_active = session.active_id == Some(id);
|
||||||
if session.close(id) {
|
if session.close(id) {
|
||||||
persist_session(&session);
|
if ws.is_primary {
|
||||||
|
persist_session(&session);
|
||||||
|
}
|
||||||
if was_active {
|
if was_active {
|
||||||
drop(session);
|
drop(session);
|
||||||
ws.render_active(&recent_files, &strings);
|
ws.render_active(&recent_files, &strings);
|
||||||
@@ -4308,7 +4462,9 @@ fn main() {
|
|||||||
};
|
};
|
||||||
let mut session = ws.session.lock().unwrap();
|
let mut session = ws.session.lock().unwrap();
|
||||||
if session.relocate(id, path) && session.activate(id) {
|
if session.relocate(id, path) && session.activate(id) {
|
||||||
persist_session(&session);
|
if ws.is_primary {
|
||||||
|
persist_session(&session);
|
||||||
|
}
|
||||||
drop(session);
|
drop(session);
|
||||||
ws.render_active(&recent_files, &strings);
|
ws.render_active(&recent_files, &strings);
|
||||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||||
@@ -4369,7 +4525,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
drop(session);
|
drop(session);
|
||||||
ws.render_active(&recent_files, &strings);
|
ws.render_active(&recent_files, &strings);
|
||||||
persist_session(&ws.session.lock().unwrap());
|
persist_window_session(ws);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TaoEvent::UserEvent(UserEvent::FileSaved(window_id, path)) => {
|
TaoEvent::UserEvent(UserEvent::FileSaved(window_id, path)) => {
|
||||||
@@ -4405,12 +4561,15 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ws.dirty = session.active().map(|tab| tab.dirty).unwrap_or(false);
|
ws.dirty = session.active().map(|tab| tab.dirty).unwrap_or(false);
|
||||||
persist_session(&session);
|
if ws.is_primary {
|
||||||
|
persist_session(&session);
|
||||||
|
}
|
||||||
update_tabs(&ws.webview, &session);
|
update_tabs(&ws.webview, &session);
|
||||||
update_window_title(&ws.window, &session);
|
update_window_title(&ws.window, &session);
|
||||||
if ws.pending_close {
|
let pending_close = ws.pending_close;
|
||||||
save_window_geom(&ws.window);
|
drop(session);
|
||||||
*control_flow = ControlFlow::Exit;
|
if pending_close {
|
||||||
|
close_window(&mut app, window_id, control_flow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TaoEvent::UserEvent(UserEvent::SaveFailed(window_id, _path, error)) => {
|
TaoEvent::UserEvent(UserEvent::SaveFailed(window_id, _path, error)) => {
|
||||||
@@ -4423,9 +4582,8 @@ fn main() {
|
|||||||
if ws.pending_close {
|
if ws.pending_close {
|
||||||
ws.pending_close = false;
|
ws.pending_close = false;
|
||||||
if confirm_discard_unsaved(&error) {
|
if confirm_discard_unsaved(&error) {
|
||||||
save_window_geom(&ws.window);
|
persist_window_session(ws);
|
||||||
persist_session(&ws.session.lock().unwrap());
|
close_window(&mut app, window_id, control_flow);
|
||||||
*control_flow = ControlFlow::Exit;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
show_warning_dialog(
|
show_warning_dialog(
|
||||||
@@ -4491,8 +4649,10 @@ fn main() {
|
|||||||
"if(window.__mdPreviewSave)window.__mdPreviewSave();",
|
"if(window.__mdPreviewSave)window.__mdPreviewSave();",
|
||||||
);
|
);
|
||||||
} else if !ws.pending_close {
|
} else if !ws.pending_close {
|
||||||
save_window_geom(&ws.window);
|
if ws.is_primary {
|
||||||
persist_session(&ws.session.lock().unwrap());
|
save_window_geom(&ws.window);
|
||||||
|
}
|
||||||
|
persist_window_session(ws);
|
||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4562,6 +4722,15 @@ fn main() {
|
|||||||
let _ = proxy.send_event(UserEvent::OpenPaths(paths, false));
|
let _ = proxy.send_event(UserEvent::OpenPaths(paths, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
TaoEvent::WindowEvent {
|
||||||
|
event: WindowEvent::Focused(true),
|
||||||
|
window_id,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if app.windows.contains_key(&window_id) {
|
||||||
|
app.active_window = window_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
TaoEvent::WindowEvent {
|
TaoEvent::WindowEvent {
|
||||||
event: WindowEvent::CloseRequested,
|
event: WindowEvent::CloseRequested,
|
||||||
window_id,
|
window_id,
|
||||||
@@ -4576,9 +4745,17 @@ fn main() {
|
|||||||
"if(window.__mdPreviewSave)window.__mdPreviewSave();",
|
"if(window.__mdPreviewSave)window.__mdPreviewSave();",
|
||||||
);
|
);
|
||||||
} else if !ws.pending_close {
|
} else if !ws.pending_close {
|
||||||
save_window_geom(&ws.window);
|
persist_window_session(ws);
|
||||||
persist_session(&ws.session.lock().unwrap());
|
close_window(&mut app, window_id, control_flow);
|
||||||
*control_flow = ControlFlow::Exit;
|
}
|
||||||
|
}
|
||||||
|
TaoEvent::WindowEvent {
|
||||||
|
event: WindowEvent::Destroyed,
|
||||||
|
window_id,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if app.windows.contains_key(&window_id) {
|
||||||
|
close_window(&mut app, window_id, control_flow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|||||||
Reference in New Issue
Block a user