Skip to content
Draft
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
Expand Up @@ -21,6 +21,8 @@ public CommandPaletteContentPageViewModel(IContentPage model, TaskScheduler sche
IFormContent form => new ContentFormViewModel(form, context),
IMarkdownContent markdown => new ContentMarkdownViewModel(markdown, context),
ITreeContent tree => new ContentTreeViewModel(tree, context),
IPlainTextContent plainText => new ContentPlainTextViewModel(plainText, context),
IImageContent image => new ContentImageViewModel(image, context),
_ => null,
};
return viewModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CmdPal.Core.ViewModels;
using Microsoft.CmdPal.Core.ViewModels.Models;
using Microsoft.CommandPalette.Extensions;

namespace Microsoft.CmdPal.UI.ViewModels;

public partial class ContentImageViewModel : ContentViewModel
{
public ExtensionObject<IImageContent> Model { get; }

public IconInfoViewModel Image { get; protected set; } = new(null);

public double MaxWidth { get; protected set; } = double.PositiveInfinity;

public double MaxHeight { get; protected set; } = double.PositiveInfinity;

public ContentImageViewModel(IImageContent content, WeakReference<IPageContext> context)
: base(context)
{
Model = new ExtensionObject<IImageContent>(content);
}

public override void InitializeProperties()
{
var model = Model.Unsafe;
if (model is null)
{
return;
}

Image = new IconInfoViewModel(model.Image);
Image.InitializeProperties();

MaxWidth = model.MaxWidth <= 0 ? double.PositiveInfinity : model.MaxWidth;
MaxHeight = model.MaxHeight <= 0 ? double.PositiveInfinity : model.MaxHeight;

UpdateProperty(nameof(Image), nameof(MaxWidth), nameof(MaxHeight));
model.PropChanged += Model_PropChanged;
}

private void Model_PropChanged(object sender, IPropChangedEventArgs args)
{
try
{
var propName = args.PropertyName;
FetchProperty(propName);
}
catch (Exception ex)
{
ShowException(ex);
}
}

private void FetchProperty(string propertyName)
{
var model = Model.Unsafe;
if (model is null)
{
return; // throw?
}

switch (propertyName)
{
case nameof(Image):
Image = new IconInfoViewModel(model.Image);
Image.InitializeProperties();
UpdateProperty(propertyName);
break;

case nameof(IImageContent.MaxWidth):
MaxWidth = model.MaxWidth <= 0 ? double.PositiveInfinity : model.MaxWidth;
UpdateProperty(propertyName);
break;

case nameof(IImageContent.MaxHeight):
MaxHeight = model.MaxHeight <= 0 ? double.PositiveInfinity : model.MaxHeight;
UpdateProperty(propertyName);
break;
}
}

protected override void UnsafeCleanup()
{
base.UnsafeCleanup();
var model = Model.Unsafe;
if (model is not null)
{
model.PropChanged -= Model_PropChanged;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CmdPal.Core.ViewModels;
using Microsoft.CmdPal.Core.ViewModels.Models;
using Microsoft.CommandPalette.Extensions;

namespace Microsoft.CmdPal.UI.ViewModels;

public partial class ContentPlainTextViewModel : ContentViewModel
{
public ExtensionObject<IPlainTextContent> Model { get; }

public string? Text { get; protected set; }

public ContentPlainTextViewModel(IPlainTextContent content, WeakReference<IPageContext> context)
: base(context)
{
Model = new ExtensionObject<IPlainTextContent>(content);
}

public override void InitializeProperties()
{
var model = Model.Unsafe;
if (model is null)
{
return;
}

Text = model.Text;
UpdateProperty(nameof(Text));
model.PropChanged += Model_PropChanged;
}

private void Model_PropChanged(object sender, IPropChangedEventArgs args)
{
try
{
var propName = args.PropertyName;
FetchProperty(propName);
}
catch (Exception ex)
{
ShowException(ex);
}
}

private void FetchProperty(string propertyName)
{
var model = Model.Unsafe;
if (model is null)
{
return; // throw?
}

Text = propertyName switch
{
nameof(Text) => model.Text,
_ => Text,
};

UpdateProperty(propertyName);
}

protected override void UnsafeCleanup()
{
base.UnsafeCleanup();
var model = Model.Unsafe;
if (model is not null)
{
model.PropChanged -= Model_PropChanged;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public override void InitializeProperties()
IFormContent form => new ContentFormViewModel(form, context),
IMarkdownContent markdown => new ContentMarkdownViewModel(markdown, context),
ITreeContent tree => new ContentTreeViewModel(tree, context),
IPlainTextContent plainText => new ContentPlainTextViewModel(plainText, context),
IImageContent image => new ContentImageViewModel(image, context),
_ => null,
};
return viewModel;
Expand Down
62 changes: 62 additions & 0 deletions src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ImageViewer.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="Microsoft.CmdPal.UI.Controls.ImageViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.CmdPal.UI.Controls"
xmlns:help="using:Microsoft.CmdPal.UI.Helpers"
mc:Ignorable="d">
<Grid Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<controls:IconBox x:Name="ZoomImage"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsTabStop="False"
RenderTransformOrigin="0.5,0.5"
SourceRequested="{x:Bind help:IconCacheProvider.SourceRequested}">
<controls:IconBox.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="ScaleTransform" ScaleX="1" ScaleY="1" />
<TranslateTransform x:Name="TranslateTransform" X="0" Y="0" />
</TransformGroup>
</controls:IconBox.RenderTransform>
</controls:IconBox>

<Border x:Name="ToolbarHost"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Margin="0,0,0,12"
Padding="8"
CornerRadius="8"
BorderThickness="1"
BorderBrush="{ThemeResource ControlStrokeColorDefaultBrush}"
Background="{ThemeResource SystemControlAcrylicWindowBrush}">
<StackPanel x:Name="Toolbar"
Orientation="Horizontal"
Spacing="8"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Transparent">
<Button Padding="8,6" Click="OnZoomInClick">
<ToolTipService.ToolTip>
<ToolTip Content="Zoom in"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more resources to localize. I think this can be done just by giving the button an x:Uid

ala:

  <data name="SettingsButton.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
    <value>Settings (Ctrl+,)</value>
  </data>

</ToolTipService.ToolTip>
<SymbolIcon Symbol="ZoomIn" />
</Button>
<Button Padding="8,6" Click="OnZoomOutClick">
<ToolTipService.ToolTip>
<ToolTip Content="Zoom out"/>
</ToolTipService.ToolTip>
<SymbolIcon Symbol="ZoomOut" />
</Button>
<Button Padding="8,6" Click="OnZoomToFitClick">
<ToolTipService.ToolTip>
<ToolTip Content="Zoom to fit"/>
</ToolTipService.ToolTip>
<FontIcon Glyph="&#xE9A6;" />
</Button>
</StackPanel>
</Border>
</Grid>
</UserControl>
Loading
Loading