Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic icons implementation #154

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Shell.ImageSource
{
public sealed class DefaultImageSourcePolicy : IImageSourcePolicy

{
public bool IsAllowed(Uri uri, Uri appUri)
{
return uri.Scheme.StartsWith("http") && uri.Host == appUri.Host;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Linq;

namespace Shell.ImageSource
{
public sealed class EnvironmentImageSourcePolicy : IImageSourcePolicy
{
private const string _allowListEnvVar = "COMPOSE_ALLOWED_IMAGE_SOURCES";
public bool IsAllowed(Uri uri, Uri appUri)
{
var allowListString = Environment.GetEnvironmentVariable(_allowListEnvVar);

// Only allow http or https sources. If no sources are allowed,
if (!uri.Scheme.StartsWith("http"))
{
return false;
}
// If the source host is the same as the app host, allow it.
if (uri.Host == appUri.Host)
{
return true;
}
if (string.IsNullOrEmpty(allowListString))
{
return false;
}

var allowedSources = allowListString.Split(';');
return allowedSources.Contains(uri.Host);
}
}
}
13 changes: 13 additions & 0 deletions Tryouts/Prototypes/Shell/ImageSource/IImageSourcePolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shell.ImageSource
{
public interface IImageSourcePolicy
{
bool IsAllowed(Uri uri, Uri appUri);
}
}
28 changes: 28 additions & 0 deletions Tryouts/Prototypes/Shell/ImageSource/ImageSourceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Windows.Media.Imaging;

namespace Shell.ImageSource
{
public class ImageSourceProvider
{
IImageSourcePolicy _imageSourcePolicy;
public ImageSourceProvider(IImageSourcePolicy imageSourcePolicy)
{
_imageSourcePolicy = imageSourcePolicy;
}

public System.Windows.Media.ImageSource? GetImageSource(Uri uri, Uri appUri)
{
if (!uri.IsAbsoluteUri)
{
uri = new Uri(appUri, uri);
}

if (_imageSourcePolicy.IsAllowed(uri, appUri))
{
return BitmapFrame.Create(uri);
}
return null;
}
}
}
12 changes: 11 additions & 1 deletion Tryouts/Prototypes/Shell/MainWebWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.IO;
using System.CommandLine.Binding;
using System.Security.Policy;
using Shell.ImageSource;

namespace Shell
{
Expand All @@ -23,14 +24,23 @@ namespace Shell
/// </summary>
public partial class MainWebWindow : Window
{
ImageSourceProvider _iconProvider = new ImageSourceProvider(new EnvironmentImageSourcePolicy());

public MainWebWindow(MainWebWindowOptions webWindowOptions)
{
InitializeComponent();

var appUrl = new Uri(webWindowOptions.Url ?? MainWebWindowOptions.DefaultUrl);
var iconUrl = webWindowOptions.IconUrl != null ? new Uri(webWindowOptions.IconUrl, UriKind.RelativeOrAbsolute) : null;

Title = webWindowOptions.Title ?? MainWebWindowOptions.DefaultTitle;
Width = webWindowOptions.Width ?? MainWebWindowOptions.DefaultWidth;
Height = webWindowOptions.Height ?? MainWebWindowOptions.DefaultHeight;
webView.Source = new Uri(webWindowOptions.Url ?? MainWebWindowOptions.DefaultUrl);
if (iconUrl != null)
{
Icon = _iconProvider.GetImageSource(iconUrl, appUrl);
}
webView.Source = appUrl;
}
}
}
1 change: 1 addition & 0 deletions Tryouts/Prototypes/Shell/MainWebWindowOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class MainWebWindowOptions
public double? Height { get; set; }
public string? Title { get; set; }
public string? Url { get; set; }
public string? IconUrl { get; set; }
public double? Width { get; set; }

public const double DefaultHeight = 450d;
Expand Down
15 changes: 9 additions & 6 deletions Tryouts/Prototypes/Shell/MainWebWindowOptionsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ namespace Shell
{
internal static class MainWebWindowOptionsParser
{
private static Option<string?> titleOption = new Option<string?>("--title", description: "Set title for window");
private static Option<string?> urlOption = new Option<string?>("--url", description: "Set url for webview. default: about:blank");
private static Option<double?> widthOption = new Option<double?>("--width", description: "Set width for window");
private static Option<double?> heightOption = new Option<double?>("--height", description: "Set height for window");
private static Option<string?> titleOption = new Option<string?>("--title", description: "Set title for the window");
private static Option<string?> urlOption = new Option<string?>("--url", description: "Set url for the webview. default: about:blank");
private static Option<double?> widthOption = new Option<double?>("--width", description: "Set width for the window");
private static Option<double?> heightOption = new Option<double?>("--height", description: "Set height for the window");
private static Option<string?> iconOption = new Option<string?>("--icon", description: "Set the icon for the window");
private static RootCommand rootCommand = new RootCommand
{
titleOption,
urlOption,
widthOption,
heightOption
heightOption,
iconOption
};

public static MainWebWindowOptions Parse(string[] args)
Expand All @@ -35,7 +37,8 @@ public static MainWebWindowOptions Parse(string[] args)
Title = parseResult.GetValueForOption(titleOption),
Url = parseResult.GetValueForOption(urlOption),
Width = parseResult.GetValueForOption(widthOption),
Height = parseResult.GetValueForOption(heightOption)
Height = parseResult.GetValueForOption(heightOption),
IconUrl = parseResult.GetValueForOption(iconOption)
};

return options;
Expand Down
36 changes: 21 additions & 15 deletions Tryouts/Prototypes/Shell/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// */

using Manifest;
using Shell.ImageSource;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -37,41 +38,46 @@ namespace Shell
/// </summary>
public partial class MainWindow : Window
{
internal List<WebContent> webContentList { get; set; } = new List<WebContent>();
private ManifestModel config;
private ModuleModel[]? modules;
internal List<WebContent> WebContentList { get; set; } = new List<WebContent>();
private ManifestModel _config;
private ModuleModel[]? _modules;
private ImageSourceProvider _iconProvider = new ImageSourceProvider(new EnvironmentImageSourcePolicy());

public MainWindow()
{
InitializeComponent();

config = ManifestParser.OpenManifestFile("exampleManifest.json");
modules = config.Modules;
DataContext = modules;
_config = ManifestParser.OpenManifestFile("exampleManifest.json");
_modules = _config.Modules;
DataContext = _modules;
}

private void CreateViews(ModuleModel item)
private void CreateViews(ModuleModel item)
{
var webContent = new WebContent(item.Url);
webContent.Title = item.AppName;
var opt = new WebContentOptions()
{
Title = item.AppName,
Uri = new Uri(item.Url),
IconUri = string.IsNullOrEmpty(item.IconUrl) ? null : new Uri(item.IconUrl)
};

var webContent = new WebContent(opt, _iconProvider);
webContent.Owner = this;

webContent.Closed += WebContent_Closed;
webContentList.Add(webContent);

WebContentList.Add(webContent);
webContent.Show();
}

private void WebContent_Closed(object? sender, EventArgs e)
{
webContentList.Remove((WebContent)sender);
WebContentList.Remove((WebContent)sender);
}

private void ShowChild_Click(object sender, RoutedEventArgs e)
{
var context = ((Button)sender).DataContext;

CreateViews((ModuleModel)context);
}
}
Expand Down
3 changes: 1 addition & 2 deletions Tryouts/Prototypes/Shell/Manifest/ModuleModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// */

using System;
using System.Collections.Generic;
using System.Text;

namespace Manifest
{
Expand All @@ -23,5 +21,6 @@ internal sealed class ModuleModel
{
public string AppName { get; set; } = string.Empty;
public string Url { get; set; } = string.Empty;
public string IconUrl { get; set; } = string.Empty;
}
}
2 changes: 1 addition & 1 deletion Tryouts/Prototypes/Shell/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Shell": {
"commandName": "Project",
"commandLineArgs": "--title=Jane --width=1800 --height=800 --url=http://google.com"
"commandLineArgs": "--title=Jane --width=1800 --height=800 --url=http://www.google.com --icon=/images/branding/googleg/1x/googleg_standard_color_128dp.png"
}
}
}
24 changes: 9 additions & 15 deletions Tryouts/Prototypes/Shell/WebContent.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,9 @@
// * and limitations under the License.
// */

using Shell.ImageSource;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Shell
{
Expand All @@ -35,11 +23,17 @@ namespace Shell
/// </summary>
public partial class WebContent : Window
{
public WebContent(string str)
public WebContent(WebContentOptions options, ImageSourceProvider iconProvider)
{
InitializeComponent();

webView.Source = new Uri(str);
webView.Source = options.Uri;
this.Title = options.Title;
if (options.IconUri != null)
{
this.Icon = iconProvider.GetImageSource(options.IconUri, options.Uri);
}

}
}
}
15 changes: 15 additions & 0 deletions Tryouts/Prototypes/Shell/WebContentOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shell
{
public sealed class WebContentOptions
{
public string Title = string.Empty;
public Uri Uri { get; set; } = new Uri("about:blank");
public Uri? IconUri { get; set; } = null;
}
}