Skip to content

Commit

Permalink
Merge pull request #152 from tryphotino/WindowsAndLinuxCustomBrowserInit
Browse files Browse the repository at this point in the history
Windows and Linux Custom Browser Init
  • Loading branch information
ottodobretsberger authored Oct 26, 2023
2 parents 599d3a4 + d410cef commit ca80982
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
38 changes: 38 additions & 0 deletions Photino.NET/PhotinoNativeParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,75 @@ internal struct PhotinoNativeParameters
///<summary>EITHER StartString or StartUrl Must be specified: Browser control will render this HTML string when initialized. Default is none.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string StartStringWide;
///<summary>EITHER StartString or StartUrl Must be specified: Browser control will render this HTML string when initialized. Default is none.</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string StartString;

///<summary>EITHER StartString or StartUrl Must be specified: Browser control will navigate to this URL when initialized. Default is none.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string StartUrlWide;
///<summary>EITHER StartString or StartUrl Must be specified: Browser control will navigate to this URL when initialized. Default is none.</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string StartUrl;

///<summary>OPTIONAL: Appears on the title bar of the native window. Default is none.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string TitleWide;
///<summary>OPTIONAL: Appears on the title bar of the native window. Default is none.</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string Title;

///<summary>WINDOWS AND LINUX ONLY: OPTIONAL: Path to a local file or a URL. Icon appears on the title bar of the native window (if supported). Default is none.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string WindowIconFileWide;
///<summary>WINDOWS AND LINUX ONLY: OPTIONAL: Path to a local file or a URL. Icon appears on the title bar of the native window (if supported). Default is none.</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string WindowIconFile;

///<summary>WINDOWS: OPTIONAL: Path to store temp files for browser control. Defaults is user's AppDataLocal folder.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string TemporaryFilesPathWide;
///<summary>WINDOWS: OPTIONAL: Path to store temp files for browser control. Defaults is user's AppDataLocal folder.</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string TemporaryFilesPath;

///<summary>OPTIONAL: Changes the user agent on the browser control at initialiation.</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string UserAgentWide;
///<summary>OPTIONAL: Changes the user agent on the browser control at initialiation.</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string UserAgent;

///<summary>OPTIONAL:
///WINDOWS: WebView2 specific string.
///https://peter.sh/experiments/chromium-command-line-switches/
///https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environmentoptions.additionalbrowserarguments?view=webview2-dotnet-1.0.1938.49&viewFallbackFrom=webview2-dotnet-1.0.1901.177view%3Dwebview2-1.0.1901.177
///https://www.chromium.org/developers/how-tos/run-chromium-with-flags/
///LINUX: Webkit2Gtk specific string.
///https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitSettings.html
///https://lazka.github.io/pgi-docs/WebKit2-4.0/classes/Settings.html
///MAC: Webkit specific string.
///https://developer.apple.com/documentation/webkit/wkwebviewconfiguration?language=objc
///https://developer.apple.com/documentation/webkit/wkpreferences?language=objc
///</summary>
[MarshalAs(UnmanagedType.LPWStr)]
internal string BrowserControlInitParametersWide;
///<summary>OPTIONAL:
///WINDOWS: WebView2 specific string.
///https://peter.sh/experiments/chromium-command-line-switches/
///https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environmentoptions.additionalbrowserarguments?view=webview2-dotnet-1.0.1938.49&viewFallbackFrom=webview2-dotnet-1.0.1901.177view%3Dwebview2-1.0.1901.177
///https://www.chromium.org/developers/how-tos/run-chromium-with-flags/
///LINUX: Webkit2Gtk specific string.
///https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitSettings.html
///https://lazka.github.io/pgi-docs/WebKit2-4.0/classes/Settings.html
///MAC: Webkit specific string.
///https://developer.apple.com/documentation/webkit/wkwebviewconfiguration?language=objc
///https://developer.apple.com/documentation/webkit/wkpreferences?language=objc
///</summary>
[MarshalAs(UnmanagedType.LPStr)]
internal string BrowserControlInitParameters;


///<summary>OPTIONAL: If native window is created from another native windowm this is the pointer to the parent window. It is set automatically in WaitforExit().</summary>
internal IntPtr NativeParent;

Expand Down Expand Up @@ -76,6 +113,7 @@ internal struct PhotinoNativeParameters
///<summary>OPTIONAL: Names of custom URL Schemes. e.g. 'app', 'custom'. Array length must be 16. Default is none.</summary>
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPWStr, SizeConst = 16)]
internal string[] CustomSchemeNamesWide;
///<summary>OPTIONAL: Names of custom URL Schemes. e.g. 'app', 'custom'. Array length must be 16. Default is none.</summary>
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPStr, SizeConst = 16)]
internal string[] CustomSchemeNames;

Expand Down
108 changes: 104 additions & 4 deletions Photino.NET/PhotinoWindow.NET.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public partial class PhotinoWindow
TemporaryFilesPathWide = IsWindowsPlatform
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Photino")
: null,
TemporaryFilesPath = IsWindowsPlatform
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Photino")
: null,
TitleWide = "Photino",
Title = "Photino",
UseOsDefaultLocation = true,
Expand Down Expand Up @@ -860,6 +857,47 @@ public Size Size
}
}

/// <summary>
/// Gets or sets platform specific initialization parameters for the native browser control on startup.
/// Default is none.
///WINDOWS: WebView2 specific string. Space separated.
///https://peter.sh/experiments/chromium-command-line-switches/
///https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environmentoptions.additionalbrowserarguments?view=webview2-dotnet-1.0.1938.49&viewFallbackFrom=webview2-dotnet-1.0.1901.177view%3Dwebview2-1.0.1901.177
///https://www.chromium.org/developers/how-tos/run-chromium-with-flags/
///LINUX: Webkit2Gtk specific string. Enter parameter names and values as JSON string.
///e.g. { "set_enable_encrypted_media": true }
///https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitSettings.html
///https://lazka.github.io/pgi-docs/WebKit2-4.0/classes/Settings.html
///MAC: Webkit specific string. Enter parameter names and values as JSON string.
///e.g. { "minimumFontSize": 8 }
///https://developer.apple.com/documentation/webkit/wkwebviewconfiguration?language=objc
///https://developer.apple.com/documentation/webkit/wkpreferences?language=objc
/// </summary>
public string BrowserControlInitParameters
{
get
{
if (IsWindowsPlatform)
return _startupParameters.BrowserControlInitParametersWide;
else
return _startupParameters.BrowserControlInitParameters;
}
set
{
var ss = IsWindowsPlatform ? _startupParameters.BrowserControlInitParametersWide : _startupParameters.BrowserControlInitParameters;
if (string.Compare(ss, value, true) != 0)
{
if (_nativeInstance == IntPtr.Zero)
if (IsWindowsPlatform)
_startupParameters.BrowserControlInitParametersWide = value;
else
_startupParameters.BrowserControlInitParameters = value;
else
throw new ApplicationException($"{nameof(ss)} cannot be changed after Photino Window is initialized");
}
}
}

/// <summary>
/// Gets or sets an HTML string that the browser control will render when initialized.
/// Default is none.
Expand Down Expand Up @@ -1704,48 +1742,110 @@ public PhotinoWindow SetGrantBrowserPermissions(bool grant)
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.UserAgent"/>. Sets the user agent on the browser control at initialization.
/// </summary>
/// <param name="userAgent"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetUserAgent(string userAgent)
{
Log($".SetUserAgent({userAgent})");
UserAgent = userAgent;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.BrowserControlInitParameters"/> platform specific initialization parameters for the native browser control on startup.
/// Default is none.
/// <remarks>
/// WINDOWS: WebView2 specific string. Space separated.
/// https://peter.sh/experiments/chromium-command-line-switches/
/// https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environmentoptions.additionalbrowserarguments?view=webview2-dotnet-1.0.1938.49&viewFallbackFrom=webview2-dotnet-1.0.1901.177view%3Dwebview2-1.0.1901.177
/// https://www.chromium.org/developers/how-tos/run-chromium-with-flags/
/// LINUX: Webkit2Gtk specific string. Enter parameter names and values as JSON string.
/// e.g. { "set_enable_encrypted_media": true }
/// https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitSettings.html
/// https://lazka.github.io/pgi-docs/WebKit2-4.0/classes/Settings.html
/// MAC: Webkit specific string. Enter parameter names and values as JSON string.
/// e.g. { "minimumFontSize": 8 }
/// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration?language=objc
/// https://developer.apple.com/documentation/webkit/wkpreferences?language=objc
/// </remarks>
/// <param name="parameters"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
/// </summary>
public PhotinoWindow SetBrowserControlInitParameters(string parameters)
{
Log($".SetBrowserControlInitParameters({parameters})");
BrowserControlInitParameters = parameters;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.MediaAutoplayEnabled"/> on the browser control at initialization.
/// </summary>
/// <param name="enable"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetMediaAutoplayEnabled(bool enable)
{
Log($".SetMediaAutoplayEnabled({enable})");
MediaAutoplayEnabled = enable;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.FileSystemAccessEnabled"/> on the browser control at initialization.
/// </summary>
/// <param name="enable"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetFileSystemAccessEnabled(bool enable)
{
Log($".SetFileSystemAccessEnabled({enable})");
FileSystemAccessEnabled = enable;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.WebSecurityEnabled"/> on the browser control at initialization.
/// </summary>
/// <param name="enable"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetWebSecurityEnabled(bool enable)
{
Log($".SetWebSecurityEnabled({enable})");
WebSecurityEnabled = enable;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.JavascriptClipboardAccessEnabled"/> on the browser control at initialization.
/// </summary>
/// <param name="enable"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetJavascriptClipboardAccessEnabled(bool enable)
{
Log($".SetJavascriptClipboardAccessEnabled({enable})");
JavascriptClipboardAccessEnabled = enable;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.MediaStreamEnabled"/> on the browser control at initialization.
/// </summary>
/// <param name="enable"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetMediaStreamEnabled(bool enable)
{
Log($".SetMediaStreamEnabled({enable})");
MediaStreamEnabled = enable;
return this;
}

/// <summary>
/// Sets <see cref="PhotinoWindow.SmoothScrollingEnabled"/> on the browser control at initialization.
/// </summary>
/// <param name="enable"></param>
/// <returns>Returns the current <see cref="PhotinoWindow"/> instance.</returns>
public PhotinoWindow SetSmoothScrollingEnabled(bool enable)
{
Log($".SetSmoothScrollingEnabled({enable})");
Expand Down Expand Up @@ -1987,7 +2087,7 @@ public PhotinoWindow SetTemporaryFilesPath(string tempFilesPath)
}

/// <summary>
/// Gets or sets the native window <see cref="PhotinoWindow.Title"/>.
/// Sets the native window <see cref="PhotinoWindow.Title"/>.
/// Default is "Photino".
/// </summary>
/// <returns>
Expand Down

0 comments on commit ca80982

Please sign in to comment.