瀏覽代碼

Merge pull request #271 from DioxusLabs/jk/desktop-no-reload

Disable reload context menu in production
Jonathan Kelley 3 年之前
父節點
當前提交
55bb62bbc8
共有 2 個文件被更改,包括 30 次插入12 次删除
  1. 12 12
      packages/desktop/src/index.html
  2. 18 0
      packages/desktop/src/lib.rs

+ 12 - 12
packages/desktop/src/index.html

@@ -1,15 +1,15 @@
 <!DOCTYPE html>
 <html>
-    <head>
-        <title>Dioxus app</title>
-        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
-    </head>
-    <body>
-        <div id="main"></div>
-        <script>
-            import("./index.js").then(function (module) {
-                module.main();
-            });
-        </script>
-    </body>
+  <head>
+    <title>Dioxus app</title>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+  </head>
+  <body>
+    <div id="main"></div>
+    <script>
+      import("./index.js").then(function (module) {
+        module.main();
+      });
+    </script>
+  </body>
 </html>

+ 18 - 0
packages/desktop/src/lib.rs

@@ -174,7 +174,25 @@ pub fn launch_with_props<P: 'static + Send>(
                 }
 
                 if cfg!(debug_assertions) {
+                    // in debug, we are okay with the reload menu showing and dev tool
                     webview = webview.with_dev_tool(true);
+                } else {
+                    // in release mode, we don't want to show the dev tool or reload menus
+                    webview = webview.with_initialization_script(
+                        r#"
+                        if (document.addEventListener) {
+                        document.addEventListener('contextmenu', function(e) {
+                            alert("You've tried to open context menu");
+                            e.preventDefault();
+                        }, false);
+                        } else {
+                        document.attachEvent('oncontextmenu', function() {
+                            alert("You've tried to open context menu");
+                            window.event.returnValue = false;
+                        });
+                        }
+                    "#,
+                    )
                 }
 
                 desktop.webviews.insert(window_id, webview.build().unwrap());