Skip to content

Commit

Permalink
feat: CTRL + T keyboard shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinguin2001 committed Jan 2, 2025
1 parent 9e15856 commit 9f3e2e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ public MainPage()
{
this.InitializeComponent();
Window.Current.SetTitleBar(CustomDragRegion);
var coreWindow = CoreWindow.GetForCurrentThread();
coreWindow.KeyDown += CoreWindow_KeyDown;

InitWebView();
}

private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
var ctrlState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control);
var isCtrlDown = (ctrlState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;

if (isCtrlDown && args.VirtualKey == VirtualKey.T)
{
CreateTab("New tab", "\uEC6C", typeof(NewTabPage));
args.Handled = true;
}
}

private async void InitWebView()
{
// preloads WebView2 for faster initial navigation
Expand Down Expand Up @@ -129,11 +144,11 @@ public void CreateTab(string header, string glyph, Type page, string url = null)

private void Tabs_TabCloseRequested(muxc.TabView sender, muxc.TabViewTabCloseRequestedEventArgs args)
{
muxc.TabViewItem selectedItem = args.Tab;
var tabcontent = (Frame)selectedItem.Content;
muxc.TabViewItem tab = args.Tab;
var tabcontent = (Frame)tab.Content;
if (tabcontent.Content is WebViewPage) (tabcontent.Content as WebViewPage).WebViewControl.Close();
if (tabcontent.Content is SplitTabPage) (tabcontent.Content as SplitTabPage).CloseWebViews();
sender.TabItems.Remove(selectedItem);
Tabs.TabItems.Remove(tab);
// Workaround for memory leak in TabView
// microsoft-ui-xaml issue #3597
// https://github.com/microsoft/microsoft-ui-xaml/issues/3597
Expand Down
13 changes: 13 additions & 0 deletions src/Pages/WebViewPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private void WebViewControl_CoreWebView2Initialized(muxc.WebView2 sender, muxc.C
sender.CoreWebView2.LaunchingExternalUriScheme += CoreWebView2_LaunchingExternalUriScheme;
sender.CoreWebView2.IsDocumentPlayingAudioChanged += CoreWebView2_IsDocumentPlayingAudioChanged;
sender.CoreWebView2.IsMutedChanged += CoreWebView2_IsMutedChanged;
sender.WebMessageReceived += Sender_WebMessageReceived;
// Apply WebView2 settings
ApplyWebView2Settings(sender);
if (launchurl != null)
Expand All @@ -73,6 +74,16 @@ private void WebViewControl_CoreWebView2Initialized(muxc.WebView2 sender, muxc.C
}
}

private void Sender_WebMessageReceived(muxc.WebView2 sender, CoreWebView2WebMessageReceivedEventArgs args)
{
// this input has been treated as VERY unsecure input
// DO NOT add anything which could be slighly insecure
if (args.TryGetWebMessageAsString() == "ControlT")
{
MainPageContent.CreateTab("New tab", "\uEC6C", typeof(NewTabPage));
}
}

private void CoreWebView2_IsMutedChanged(CoreWebView2 sender, object args)
{
if (sender.IsMuted)
Expand Down Expand Up @@ -120,6 +131,8 @@ private async void CoreWebView2_NavigationCompleted(CoreWebView2 sender, CoreWeb
string jscript = await Modules.ForceDark.ForceDarkHelper.GetForceDarkScriptAsync();
await sender.ExecuteScriptAsync(jscript);
}
string mainscript = "document.addEventListener('keydown', function(event) { if (event.ctrlKey && event.key === 't') { event.preventDefault(); window.chrome.webview.postMessage('ControlT'); } });";
await sender.ExecuteScriptAsync(mainscript);
LoadingBar.Visibility = Visibility.Collapsed;
LoadingBar.IsIndeterminate = false;
}
Expand Down

0 comments on commit 9f3e2e9

Please sign in to comment.