Skip to content
This repository was archived by the owner on Jan 15, 2023. It is now read-only.

Commit cf9eb16

Browse files
committed
Fixing Fullscreen/Kiosk issue from #237.
1 parent bac912b commit cf9eb16

10 files changed

Lines changed: 317 additions & 268 deletions

src/Chromely.CefSharp/NativeHost/ChromelyFramelessHost.cs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ protected override void OnCreated(IntPtr hWnd)
2727
{
2828
base.OnCreated(hWnd);
2929

30+
if (_options.Fullscreen || _options.KioskMode)
31+
{
32+
throw new NotSupportedException("Fullscreen/Kiok mode is not supported in ChromelyFramelessApp. Please use ChromelyBasicApp instead.");
33+
}
34+
3035
_options.WindowFrameless = true;
3136
_framelessInfo = new FramelessInfo(hWnd);
3237
_framelessOption = _options.FramelessOption;
@@ -36,34 +41,9 @@ protected override void OnCreated(IntPtr hWnd)
3641
_dwmFramelessController.HandleThemechanged();
3742
}
3843

39-
public override void ResizeBrowser(IntPtr browserHande, int width, int height)
40-
{
41-
var noResize = (_options == null) ? false : _options.DisableResizing;
42-
var gripSize = (_options?.FramelessOption == null) ? 0 : _options.FramelessOption.ResizeGrip;
43-
var addMargins = !noResize &&
44-
(gripSize > 0) &&
45-
(browserHande != IntPtr.Zero) &&
46-
(width > (4 * gripSize)) &&
47-
(height > (4 * gripSize));
48-
49-
if (addMargins)
50-
{
51-
var left = gripSize;
52-
var top = gripSize;
53-
width = width - (2 * gripSize);
54-
height = height - (2 * gripSize);
55-
56-
SetWindowPos(browserHande, IntPtr.Zero, left, top, width, height, SWP.NOZORDER);
57-
}
58-
else
59-
{
60-
SetWindowPos(browserHande, IntPtr.Zero, 0, 0, width, height, SWP.NOZORDER);
61-
}
62-
}
63-
6444
protected override WindowStylePlacement GetWindowStylePlacement(WindowState state)
6545
{
66-
WindowStylePlacement windowStyle = new WindowStylePlacement();
46+
WindowStylePlacement windowStyle = new WindowStylePlacement(_options);
6747
if (_options.UseCustomStyle && _options != null && _options.CustomStyle.IsValid())
6848
{
6949
return GetWindowStyles(_options.CustomStyle, state);
@@ -87,17 +67,39 @@ protected override WindowStylePlacement GetWindowStylePlacement(WindowState stat
8767
windowStyle.ShowCommand = SW.SHOWMAXIMIZED;
8868
break;
8969

90-
case WindowState.Fullscreen:
91-
windowStyle.ShowCommand = SW.SHOWMAXIMIZED;
92-
break;
93-
9470
default:
71+
windowStyle.ShowCommand = SW.SHOWNORMAL;
9572
break;
9673
}
9774

9875
return windowStyle;
9976
}
10077

78+
public override void ResizeBrowser(IntPtr browserHande, int width, int height)
79+
{
80+
var noResize = (_options == null) ? false : _options.DisableResizing;
81+
var gripSize = (_options?.FramelessOption == null) ? 0 : _options.FramelessOption.ResizeGrip;
82+
var addMargins = !noResize &&
83+
(gripSize > 0) &&
84+
(browserHande != IntPtr.Zero) &&
85+
(width > (4 * gripSize)) &&
86+
(height > (4 * gripSize));
87+
88+
if (addMargins)
89+
{
90+
var left = gripSize;
91+
var top = gripSize;
92+
width = width - (2 * gripSize);
93+
height = height - (2 * gripSize);
94+
95+
SetWindowPos(browserHande, IntPtr.Zero, left, top, width, height, SWP.NOZORDER);
96+
}
97+
else
98+
{
99+
SetWindowPos(browserHande, IntPtr.Zero, 0, 0, width, height, SWP.NOZORDER);
100+
}
101+
}
102+
101103
#region Frameless WndProc
102104

103105
protected override IntPtr WndProc(IntPtr hWnd, uint message, IntPtr wParam, IntPtr lParam)
@@ -194,6 +196,7 @@ be enough to prevent painting when theming is enabled. */
194196
case WM.THEMECHANGED:
195197
_dwmFramelessController?.HandleThemechanged();
196198
break;
199+
197200
case WM.WINDOWPOSCHANGED:
198201
WINDOWPOS windPos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
199202
_dwmFramelessController?.HandleWindowPosChanged(windPos);

src/Chromely.CefSharp/NativeHost/ChromelyHost.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ namespace Chromely.CefSharp.NativeHost
55
{
66
internal class ChromelyHost : NativeHostBase
77
{
8+
public ChromelyHost(IKeyboadHookHandler keyboadHandler = null) : base(keyboadHandler)
9+
{
10+
}
811
}
9-
}
12+
}

src/Chromely.CefSharp/NativeHost/Hooks/KeyboardLLHook.cs

Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,41 @@
22
// Use of this source code is governed by Chromely MIT licensed and CefSharp BSD-style license that can be found in the LICENSE file.
33

44
using Chromely.Core.Configuration;
5+
using System;
56
using System.Runtime.InteropServices;
67
using static Chromely.Interop.User32;
78

89
namespace Chromely.CefSharp.NativeHost
910
{
1011
public class KeyboardLLHook : WindowsHookBase
11-
{
12+
{
13+
protected IntPtr _handler;
1214
protected IWindowOptions _options;
15+
protected IKeyboadHookHandler _keyboadHandler;
1316

14-
public KeyboardLLHook(IWindowOptions options) : base(WH.KEYBOARD_LL)
15-
{
17+
public KeyboardLLHook(IntPtr handler, IWindowOptions options, IKeyboadHookHandler keyboadHandler) : base(WH.KEYBOARD_LL)
18+
{
19+
_handler = handler;
1620
_options = options;
21+
_keyboadHandler = keyboadHandler;
1722
HookEventHandler = OnKeyboardEvent;
18-
}
19-
20-
protected virtual bool OnKeyboardEvent(HookEventArgs args)
21-
{
22-
if (args == null)
23-
{
24-
return false;
25-
}
26-
27-
WM wParam = (WM)args.wParam.ToInt32();
28-
var hookInfo = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(args.lParam);
29-
var key = (Keys)hookInfo.vkCode;
30-
31-
32-
bool alt = IsKeyPressed(Keys.Menu);
33-
bool control = IsKeyPressed(Keys.ControlKey);
34-
35-
if (!AllowKeyboardInput(alt, control, (int)key))
36-
{
37-
return true;
38-
}
39-
40-
return false;
41-
}
42-
43-
/// <summary>Determines whether the specified keyboard input should be allowed to be processed by the system.</summary>
44-
/// <remarks>Helps block unwanted keys and key combinations that could exit the app, make system changes, etc.</remarks>
45-
protected virtual bool AllowKeyboardInput(bool alt, bool control, int intKey)
46-
{
47-
if (_options.KioskMode)
48-
{
49-
return AllowKeyboardInputForKioskMode(alt, control, intKey);
50-
}
51-
52-
return true;
53-
}
54-
55-
#region
23+
}
5624

57-
private bool AllowKeyboardInputForKioskMode(bool alt, bool control, int intKey)
25+
protected virtual bool OnKeyboardEvent(HookEventArgs args)
5826
{
59-
if (!_options.KioskMode)
60-
{
61-
return true;
62-
}
63-
64-
Keys key = (Keys)intKey;
65-
66-
// Disallow various special keys.
67-
if (key <= Keys.Back || key == Keys.NoName ||
68-
key == Keys.Menu || key == Keys.Pause ||
69-
key == Keys.Help)
27+
if (args == null)
7028
{
7129
return false;
7230
}
7331

74-
// Disallow ranges of special keys.
75-
// Currently leaves volume controls enabled; consider if this makes sense.
76-
// Disables non-existing Keys up to 65534, to err on the side of caution for future keyboard expansion.
77-
if ((key >= Keys.LWin && key <= Keys.Sleep) ||
78-
(key >= Keys.KanaMode && key <= Keys.HanjaMode) ||
79-
(key >= Keys.IMEConvert && key <= Keys.IMEModeChange) ||
80-
//(key >= VirtualKey.BROWSER_BACK && key <= VirtualKey.BROWSER_HOME) ||
81-
(key >= Keys.MediaNextTrack && key <= Keys.LaunchApplication2) ||
82-
(key >= Keys.ProcessKey && key <= (Keys)65534))
83-
{
84-
return false;
85-
}
32+
WM wParam = (WM)args.wParam.ToInt32();
33+
var hookInfo = Marshal.PtrToStructure<KBDLLHOOKSTRUCT>(args.lParam);
34+
var key = (Keys)hookInfo.vkCode;
8635

87-
// Disallow specific key combinations. (These component keys would be OK on their own.)
88-
if ((alt && key == Keys.Tab) ||
89-
(alt && key == Keys.Space) ||
90-
(control && key == Keys.Escape))
91-
{
92-
return false;
93-
}
36+
bool alt = IsKeyPressed(Keys.Menu);
37+
bool control = IsKeyPressed(Keys.ControlKey);
9438

95-
// Allow anything else (like letters, numbers, spacebar, braces, and so on).
96-
return true;
39+
return _keyboadHandler.HandleKey(_handler, new KeyboardParam(wParam == WM.KEYUP, alt, control, key));
9740
}
98-
99-
#endregion
10041
}
10142
}

src/Chromely.CefSharp/NativeHost/Hooks/WindowsHookBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public WindowsHookBase(WH hook, HOOKPROC func)
3131

3232
protected IntPtr CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
3333
{
34-
if (code < 0)
34+
if (code < 0 || code == (int)HC.NOREMOVE)
3535
{
3636
return CallNextHookEx(_hookID, (HC)code, wParam, lParam);
3737
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Chromely.Core.Configuration;
2+
using Chromely.Core.Host;
3+
using System;
4+
using static Chromely.Interop.User32;
5+
6+
namespace Chromely.CefSharp.NativeHost
7+
{
8+
internal class DefaulKeyboadHookHandler : IKeyboadHookHandler
9+
{
10+
protected IChromelyNativeHost _nativeHost;
11+
protected IWindowOptions _options;
12+
13+
public DefaulKeyboadHookHandler(IChromelyNativeHost nativeHost, IWindowOptions options)
14+
{
15+
_nativeHost = nativeHost;
16+
_options = options;
17+
}
18+
19+
public bool HandleKey(IntPtr hWnd, object param)
20+
{
21+
KeyboardParam keyboardParam = param as KeyboardParam;
22+
if (keyboardParam == null)
23+
{
24+
return false;
25+
}
26+
27+
if (!AllowKeyboardInput(keyboardParam.Alt, keyboardParam.Control, keyboardParam.Key))
28+
{
29+
return true;
30+
}
31+
32+
switch (keyboardParam.Key)
33+
{
34+
case Keys.F4:
35+
{
36+
if (!_options.WindowFrameless && _options.KioskMode)
37+
{
38+
return true;
39+
}
40+
break;
41+
}
42+
case Keys.F11:
43+
{
44+
if (!_options.WindowFrameless && !_options.KioskMode && keyboardParam.IsKeyUp)
45+
{
46+
_nativeHost.ToggleFullscreen(hWnd);
47+
return true;
48+
}
49+
break;
50+
}
51+
}
52+
53+
return false;
54+
}
55+
56+
/// <summary>Determines whether the specified keyboard input should be allowed to be processed by the system.</summary>
57+
/// <remarks>Helps block unwanted keys and key combinations that could exit the app, make system changes, etc.</remarks>
58+
private bool AllowKeyboardInput(bool alt, bool control, Keys key)
59+
{
60+
if (_options.KioskMode)
61+
{
62+
return AllowKeyboardInputForKioskMode(alt, control, key);
63+
}
64+
65+
return true;
66+
}
67+
68+
#region
69+
70+
private bool AllowKeyboardInputForKioskMode(bool alt, bool control, Keys key)
71+
{
72+
if (!_options.KioskMode)
73+
{
74+
return true;
75+
}
76+
77+
// Disallow various special keys.
78+
if (key <= Keys.Back || key == Keys.NoName ||
79+
key == Keys.Menu || key == Keys.Pause ||
80+
key == Keys.Help)
81+
{
82+
return false;
83+
}
84+
85+
// Disallow ranges of special keys.
86+
// Currently leaves volume controls enabled; consider if this makes sense.
87+
// Disables non-existing Keys up to 65534, to err on the side of caution for future keyboard expansion.
88+
if ((key >= Keys.LWin && key <= Keys.Sleep) ||
89+
(key >= Keys.KanaMode && key <= Keys.HanjaMode) ||
90+
(key >= Keys.IMEConvert && key <= Keys.IMEModeChange) ||
91+
//(key >= VirtualKey.BROWSER_BACK && key <= VirtualKey.BROWSER_HOME) ||
92+
(key >= Keys.MediaNextTrack && key <= Keys.LaunchApplication2) ||
93+
(key >= Keys.ProcessKey && key <= (Keys)65534))
94+
{
95+
return false;
96+
}
97+
98+
// Disallow specific key combinations. (These component keys would be OK on their own.)
99+
if ((alt && key == Keys.Tab) ||
100+
(alt && key == Keys.Space) ||
101+
(control && key == Keys.Escape))
102+
{
103+
return false;
104+
}
105+
106+
// Allow anything else (like letters, numbers, spacebar, braces, and so on).
107+
return true;
108+
}
109+
110+
#endregion
111+
}
112+
113+
internal class KeyboardParam
114+
{
115+
public KeyboardParam(bool isKeyUp, bool alt, bool control, Keys key)
116+
{
117+
IsKeyUp = isKeyUp;
118+
Alt = alt;
119+
Control = control;
120+
Key = key;
121+
}
122+
public bool IsKeyUp { get; set; }
123+
public bool Alt { get; set; }
124+
public bool Control { get; set; }
125+
public Keys Key { get; set; }
126+
}
127+
}

0 commit comments

Comments
 (0)