fix(CasdoorLoginWindow): 修复登录界面右键菜单问题
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -46,4 +46,5 @@ bin/
|
||||
logs/
|
||||
|
||||
### JCEF Dlls ###
|
||||
library/jcef/
|
||||
library/*/
|
||||
|
||||
|
||||
BIN
library/WallpaperOptimization.dll
Normal file
BIN
library/WallpaperOptimization.dll
Normal file
Binary file not shown.
BIN
library/chrome_elf.dll
Normal file
BIN
library/chrome_elf.dll
Normal file
Binary file not shown.
@@ -32,6 +32,7 @@ import org.cef.handler.CefKeyboardHandlerAdapter;
|
||||
import org.cef.handler.CefLifeSpanHandlerAdapter;
|
||||
import org.cef.handler.CefLoadHandlerAdapter;
|
||||
import org.cef.callback.CefMenuModel;
|
||||
import org.cef.callback.CefMenuModel.MenuId;
|
||||
import org.cef.callback.CefContextMenuParams;
|
||||
|
||||
|
||||
@@ -195,71 +196,34 @@ public class CasdoorLoginWindow {
|
||||
|
||||
// 🔹 自定义右键菜单
|
||||
client.addContextMenuHandler(new org.cef.handler.CefContextMenuHandlerAdapter() {
|
||||
private final int CMD_COPY = 100;
|
||||
private final int CMD_BACK = 101;
|
||||
private final int CMD_FORWARD = 102;
|
||||
private final int CMD_RELOAD = 103;
|
||||
|
||||
@Override
|
||||
public void onBeforeContextMenu(org.cef.browser.CefBrowser browser, org.cef.browser.CefFrame frame,
|
||||
org.cef.callback.CefContextMenuParams params, org.cef.callback.CefMenuModel model) {
|
||||
model.clear(); // 清空默认菜单
|
||||
boolean hasSelection = false;
|
||||
try {
|
||||
java.lang.reflect.Method m = params.getClass().getMethod("getSelectionText");
|
||||
Object sel = m.invoke(params);
|
||||
if (sel != null && !sel.toString().trim().isEmpty()) {
|
||||
hasSelection = true;
|
||||
}
|
||||
} catch (Throwable ignored) {}
|
||||
if (hasSelection) {
|
||||
model.addItem(CMD_COPY, "复制");
|
||||
if (browser.canGoBack()) {
|
||||
model.addItem(MenuId.MENU_ID_BACK, "后退");
|
||||
}
|
||||
if (browser.canGoForward()) {
|
||||
model.addItem(MenuId.MENU_ID_FORWARD, "前进");
|
||||
}
|
||||
// 仅有选中内容时显示“复制”
|
||||
String selectionText = params.getSelectionText();
|
||||
if (selectionText != null && !selectionText.trim().isEmpty()) {
|
||||
model.addItem(MenuId.MENU_ID_COPY, "复制");
|
||||
}
|
||||
// 仅在可编辑区域显示“粘贴”
|
||||
if (params.isEditable()) {
|
||||
model.addItem(MenuId.MENU_ID_PASTE, "粘贴");
|
||||
}
|
||||
model.addItem(MenuId.MENU_ID_RELOAD, "刷新");
|
||||
|
||||
// 可后退/前进时显示
|
||||
try {
|
||||
if (browser.canGoBack()) {
|
||||
model.addItem(CMD_BACK, "后退");
|
||||
}
|
||||
if (browser.canGoForward()) {
|
||||
model.addItem(CMD_FORWARD, "前进");
|
||||
}
|
||||
} catch (Throwable ignored) {}
|
||||
|
||||
model.addItem(CMD_RELOAD, "刷新");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onContextMenuCommand(org.cef.browser.CefBrowser browser, org.cef.browser.CefFrame frame,
|
||||
org.cef.callback.CefContextMenuParams params, int commandId, int eventFlags) {
|
||||
switch (commandId) {
|
||||
case CMD_COPY:
|
||||
// 通过 JS 将选中内容复制到剪贴板(包含降级方案)
|
||||
String copyJs =
|
||||
"(function(){"
|
||||
+ "var s = window.getSelection ? window.getSelection().toString() : (document.selection ? document.selection.createRange().text : '');"
|
||||
+ "if(!s) return;"
|
||||
+ "try{ navigator.clipboard.writeText(s); }"
|
||||
+ "catch(e){ var ta=document.createElement('textarea'); ta.value=s; document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta);} "
|
||||
+ "})();";
|
||||
browser.executeJavaScript(copyJs, browser.getURL(), 0);
|
||||
return true;
|
||||
|
||||
case CMD_BACK:
|
||||
try { if (browser.canGoBack()) browser.goBack(); } catch (Throwable ignored) {}
|
||||
return true;
|
||||
|
||||
case CMD_FORWARD:
|
||||
try { if (browser.canGoForward()) browser.goForward(); } catch (Throwable ignored) {}
|
||||
return true;
|
||||
|
||||
case CMD_RELOAD:
|
||||
try { browser.reload(); } catch (Throwable ignored) {}
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
org.cef.callback.CefContextMenuParams params, int commandId, int eventFlags) {
|
||||
return false; // 使用默认处理
|
||||
}
|
||||
});
|
||||
} catch (Throwable e) {
|
||||
|
||||
Reference in New Issue
Block a user