windows.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. use crate::{
  2. constants::{UPDATER_ENABLED, window_state_flags},
  3. server::get_wsl_config,
  4. };
  5. use std::{ops::Deref, time::Duration};
  6. use tauri::{AppHandle, Manager, Runtime, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
  7. use tauri_plugin_window_state::AppHandleExt;
  8. use tokio::sync::mpsc;
  9. pub struct MainWindow(WebviewWindow);
  10. impl Deref for MainWindow {
  11. type Target = WebviewWindow;
  12. fn deref(&self) -> &Self::Target {
  13. &self.0
  14. }
  15. }
  16. impl MainWindow {
  17. pub const LABEL: &str = "main";
  18. pub fn create(app: &AppHandle) -> Result<Self, tauri::Error> {
  19. if let Some(window) = app.get_webview_window(Self::LABEL) {
  20. return Ok(Self(window));
  21. }
  22. let wsl_enabled = get_wsl_config(app.clone())
  23. .ok()
  24. .map(|v| v.enabled)
  25. .unwrap_or(false);
  26. let window_builder = base_window_config(
  27. WebviewWindowBuilder::new(app, Self::LABEL, WebviewUrl::App("/".into())),
  28. app,
  29. )
  30. .title("OpenCode")
  31. .decorations(true)
  32. .disable_drag_drop_handler()
  33. .zoom_hotkeys_enabled(false)
  34. .visible(true)
  35. .maximized(true)
  36. .initialization_script(format!(
  37. r#"
  38. window.__OPENCODE__ ??= {{}};
  39. window.__OPENCODE__.updaterEnabled = {UPDATER_ENABLED};
  40. window.__OPENCODE__.wsl = {wsl_enabled};
  41. "#
  42. ));
  43. let window = window_builder.build()?;
  44. setup_window_state_listener(app, &window);
  45. #[cfg(windows)]
  46. {
  47. use tauri_plugin_decorum::WebviewWindowExt;
  48. let _ = window.create_overlay_titlebar();
  49. }
  50. Ok(Self(window))
  51. }
  52. }
  53. fn setup_window_state_listener(app: &AppHandle, window: &WebviewWindow) {
  54. let (tx, mut rx) = mpsc::channel::<()>(1);
  55. window.on_window_event(move |event| {
  56. use tauri::WindowEvent;
  57. if !matches!(event, WindowEvent::Moved(_) | WindowEvent::Resized(_)) {
  58. return;
  59. }
  60. let _ = tx.try_send(());
  61. });
  62. tokio::spawn({
  63. let app = app.clone();
  64. async move {
  65. let save = || {
  66. let handle = app.clone();
  67. let app = app.clone();
  68. let _ = handle.run_on_main_thread(move || {
  69. let _ = app.save_window_state(window_state_flags());
  70. });
  71. };
  72. while rx.recv().await.is_some() {
  73. tokio::time::sleep(Duration::from_millis(200)).await;
  74. save();
  75. }
  76. }
  77. });
  78. }
  79. pub struct LoadingWindow(WebviewWindow);
  80. impl Deref for LoadingWindow {
  81. type Target = WebviewWindow;
  82. fn deref(&self) -> &Self::Target {
  83. &self.0
  84. }
  85. }
  86. impl LoadingWindow {
  87. pub const LABEL: &str = "loading";
  88. pub fn create(app: &AppHandle) -> Result<Self, tauri::Error> {
  89. let window_builder = base_window_config(
  90. WebviewWindowBuilder::new(app, Self::LABEL, tauri::WebviewUrl::App("/loading".into())),
  91. app,
  92. )
  93. .center()
  94. .resizable(false)
  95. .inner_size(640.0, 480.0)
  96. .visible(true);
  97. Ok(Self(window_builder.build()?))
  98. }
  99. }
  100. fn base_window_config<'a, R: Runtime, M: Manager<R>>(
  101. window_builder: WebviewWindowBuilder<'a, R, M>,
  102. _app: &AppHandle,
  103. ) -> WebviewWindowBuilder<'a, R, M> {
  104. let window_builder = window_builder.decorations(true);
  105. #[cfg(windows)]
  106. let window_builder = window_builder
  107. // Some VPNs set a global/system proxy that WebView2 applies even for loopback
  108. // connections, which breaks the app's localhost sidecar server.
  109. // Note: when setting additional args, we must re-apply wry's default
  110. // `--disable-features=...` flags.
  111. .additional_browser_args(
  112. "--proxy-bypass-list=<-loopback> --disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection",
  113. )
  114. .data_directory(_app.path().config_dir().expect("Failed to get config dir").join(_app.config().product_name.clone().unwrap()))
  115. .decorations(false);
  116. #[cfg(target_os = "macos")]
  117. let window_builder = window_builder
  118. .title_bar_style(tauri::TitleBarStyle::Overlay)
  119. .hidden_title(true)
  120. .traffic_light_position(tauri::LogicalPosition::new(12.0, 18.0));
  121. window_builder
  122. }