windows.rs 4.4 KB

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