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

WPF ZoomIn/Out Functionality #306

Merged
merged 3 commits into from
Apr 14, 2014
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
22 changes: 22 additions & 0 deletions CefSharp.Core/ManagedCefBrowserAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,28 @@ namespace CefSharp
}
}

double GetZoomLevel()
{
auto cefHost = _renderClientAdapter->TryGetCefHost();

if (cefHost != nullptr)
Copy link
Member

Choose a reason for hiding this comment

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

The whitespace here is quite messed up. Could you take a look at it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting! The code looks fine in visual studio, I can see the formatting of the patch is clearly all over the place. I'm fairly new to the world of Git, so if you have any insights they would be greatly appreciated, otherwise I'll take a look at the issue shortly.

Copy link
Contributor

Choose a reason for hiding this comment

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

@amaitland we use spaces for whitespace here - not tabs. I guess that's the problem ... Take a look at your Visual Studio settings (don't know if it's configurable per solution/project). Otherwise I can reccomend the PowerCommands aka "Productivity Power Tools" VS add-in. It complains if a file has mixed tabs/spaces - and you just click a button to fix it in either direction.

Copy link
Member

Choose a reason for hiding this comment

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

But be very careful to not commit a lot of whitespace changes along with other changes (which is easy if you click the "fix tabs/spaces" button 😃). Please fix as needed, but as separate pull requests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes perfect sense, thank you both for the prompt reply's. I've just committed the formatting changes.

{
return cefHost->GetZoomLevel();
}

return 0;
}

void SetZoomLevel(double zoomLevel)
{
auto cefHost = _renderClientAdapter->TryGetCefHost();

if (cefHost != nullptr)
{
cefHost->SetZoomLevel(zoomLevel);
}
}

virtual void Error( Exception^ ex )
{

Expand Down
7 changes: 7 additions & 0 deletions CefSharp.Wpf.Example/Views/Main/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@
<MenuItem Header="Forward"
Command="{Binding WebBrowser.ForwardCommand}" />
<Separator />
<MenuItem Header="Zoom In"
Command="{Binding WebBrowser.ZoomInCommand}" />
<MenuItem Header="Zoom Out"
Command="{Binding WebBrowser.ZoomOutCommand}" />
<MenuItem Header="Zoom Reset"
Command="{Binding WebBrowser.ZoomResetCommand}" />
<Separator />
<MenuItem Header="View Source"
Command="{Binding ViewSourceCommand}" />
</ContextMenu>
Expand Down
24 changes: 22 additions & 2 deletions CefSharp.Wpf/IWpfWebBrowser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Input;

namespace CefSharp.Wpf
{
Expand Down Expand Up @@ -43,6 +42,21 @@ public interface IWpfWebBrowser : IWebBrowser
/// </summary>
ICommand ReloadCommand { get; }

/// <summary>
/// Command which increases the zoom level
/// </summary>
ICommand ZoomInCommand { get; }

/// <summary>
/// Command which decreases the zoom level
/// </summary>
ICommand ZoomOutCommand { get; }

/// <summary>
/// Command which resets the zoom level to default
/// </summary>
ICommand ZoomResetCommand { get; }

/// <summary>
/// Opens up a new program window (using the default text editor) where the source code of the currently displayed web
/// page is shown.
Expand All @@ -55,5 +69,11 @@ public interface IWpfWebBrowser : IWebBrowser
/// <returns><c>true</c> if keyboard focus and logical focus were set to this element; <c>false</c> if only logical focus
/// was set to this element, or if the call to this method did not force the focus to change.</returns>
bool Focus();

/// <summary>
/// The zoom level at which the browser control is currently displaying. Can be set to 0 to clear the zoom level(resets to default zoom level)
Copy link
Member

Choose a reason for hiding this comment

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

I lack a space here, but since this is such a minor change I'll fix it myself.

/// </summary>
/// <remarks>This property is a Dependency Property and fully supports data binding.</remarks>
double ZoomLevel { get; set; }
}
}
88 changes: 86 additions & 2 deletions CefSharp.Wpf/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class WebView : ContentControl, IRenderWebBrowser, IWpfWebBrowser
public ICommand BackCommand { get; private set; }
public ICommand ForwardCommand { get; private set; }
public ICommand ReloadCommand { get; private set; }
public ICommand ZoomInCommand { get; private set; }
public ICommand ZoomOutCommand { get; private set; }
public ICommand ZoomResetCommand { get; private set; }

public bool CanGoBack { get; private set; }
public bool CanGoForward { get; private set; }
Expand Down Expand Up @@ -144,7 +147,61 @@ public string Title
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(WebView), new PropertyMetadata(defaultValue: null));

#endregion CleanupElement dependency property
#endregion Title dependency property

#region ZoomLevel dependency property

/// <summary>
/// The zoom level at which the browser control is currently displaying. Can be set to 0 to clear the zoom level(resets to default zoom level)
/// </summary>
/// <remarks>This property is a Dependency Property and fully supports data binding.</remarks>
public double ZoomLevel
{
get { return (double)GetValue(ZoomLevelProperty); }
set { SetValue(ZoomLevelProperty, value); }
}

public static readonly DependencyProperty ZoomLevelProperty =
DependencyProperty.Register("ZoomLevel", typeof(double), typeof(WebView),
new UIPropertyMetadata(0d, OnZoomLevelChanged));

private static void OnZoomLevelChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var owner = (WebView)sender;
var oldValue = (double)args.OldValue;
var newValue = (double)args.NewValue;

owner.OnZoomLevelChanged(oldValue, newValue);
}

protected virtual void OnZoomLevelChanged(double oldValue, double newValue)
{
if (!Cef.IsInitialized)
{
throw new InvalidOperationException("Cef::IsInitialized is false");
}

managedCefBrowserAdapter.SetZoomLevel(newValue);
}

#endregion ZoomLevel dependency property

#region ZoomLevelIncrement dependency property

/// <summary>
/// Specifies the amount used to increase/decrease to ZoomLevel by
/// By Default this value is 0.10
/// </summary>
public double ZoomLevelIncrement
{
get { return (double)GetValue(ZoomLevelIncrementProperty); }
set { SetValue(ZoomLevelIncrementProperty, value); }
}

public static readonly DependencyProperty ZoomLevelIncrementProperty =
DependencyProperty.Register("ZoomLevelIncrement", typeof(double), typeof(WebView), new PropertyMetadata(0.10));

#endregion ZoomLevelIncrement dependency property

#region CleanupElement dependency property

Expand Down Expand Up @@ -203,7 +260,7 @@ protected virtual void Cleanup()
}
}

#endregion Title dependency property
#endregion CleanupElement dependency property

#region TooltipText dependency property

Expand Down Expand Up @@ -275,6 +332,9 @@ public WebView()
BackCommand = new DelegateCommand(Back, () => CanGoBack);
ForwardCommand = new DelegateCommand(Forward, () => CanGoForward);
ReloadCommand = new DelegateCommand(Reload, () => CanReload);
ZoomInCommand = new DelegateCommand(ZoomIn);
ZoomOutCommand = new DelegateCommand(ZoomOut);
ZoomResetCommand = new DelegateCommand(ZoomReset);

managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this);
managedCefBrowserAdapter.CreateOffscreenBrowser(BrowserSettings ?? new BrowserSettings());
Expand Down Expand Up @@ -748,6 +808,30 @@ public void Reload()
managedCefBrowserAdapter.Reload();
}

private void ZoomIn()
{
DoInUi(() =>
{
ZoomLevel = ZoomLevel + ZoomLevelIncrement;
});
}

private void ZoomOut()
{
DoInUi(() =>
{
ZoomLevel = ZoomLevel - ZoomLevelIncrement;
});
}

private void ZoomReset()
{
DoInUi(() =>
{
ZoomLevel = 0;
});
}

public void ShowDevTools()
{
// TODO: Do something about this one.
Expand Down