Skip to content
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
36 changes: 35 additions & 1 deletion src/Modern.Forms/ControlPaint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ public static void DrawMaximizeGlyph (PaintEventArgs e, Rectangle rectangle)
e.Canvas.DrawRectangle (rectangle, Theme.ForegroundColorOnAccent);
}

/// <summary>
/// Draws a restore glyph, as seen on FormTitleBar when the window is maximized.
/// </summary>
public static void DrawRestoreGlyph (PaintEventArgs e, Rectangle rectangle)
{
var color = Theme.ForegroundColorOnAccent;
var offset = e.LogicalToDeviceUnits (2);

var back = new Rectangle (
rectangle.X + offset,
rectangle.Y,
rectangle.Width - offset - 1,
rectangle.Height - offset - 1);

var front = new Rectangle (
rectangle.X,
rectangle.Y + offset,
rectangle.Width - offset,
rectangle.Height - offset);

// Draw "front" "window"
e.Canvas.DrawRectangle (front, color);

// Draw "back" "window"
using var path = new SKPath ();

path.MoveTo (back.Left, front.Top);
path.LineTo (back.Left, back.Top);
path.LineTo (back.Right, back.Top);
path.LineTo (back.Right, back.Bottom);
path.LineTo (front.Right, back.Bottom);

e.Canvas.DrawPath (path, color);
}

/// <summary>
/// Draws a minimize glyph, as seen on FormTitleBar.
/// </summary>
Expand All @@ -114,7 +149,6 @@ public static void DrawMinimizeGlyph (PaintEventArgs e, Rectangle rectangle)
e.Canvas.DrawLine (rectangle.X, rectangle.Y, rectangle.Right, rectangle.Y, Theme.ForegroundColorOnAccent);
}


/// <summary>
/// Draws a RadioButton glyph.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Modern.Forms/Extensions/SkiaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ public static void DrawLine (this SKCanvas canvas, float x1, float y1, float x2,
canvas.DrawLine (x1, y1, x2, y2, paint);
}

/// <summary>
/// Draws a path.
/// </summary>
public static void DrawPath (this SKCanvas canvas, SKPath path, SKColor color, int thickness = 1)
{
using var paint = new SKPaint { Color = color, StrokeWidth = thickness, IsStroke = true };

canvas.DrawPath (path, paint);
}

/// <summary>
/// Draws an unfilled rectangle.
/// </summary>
Expand Down
54 changes: 48 additions & 6 deletions src/Modern.Forms/FormTitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ public FormTitleBar ()
minimize_button.Click += (o, e) => {
var form_min = FindForm ();

if (form_min != null)
if (form_min is not null)
form_min.WindowState = FormWindowState.Minimized;
};

maximize_button = Controls.AddImplicitControl (new TitleBarButton (TitleBarButton.TitleBarButtonGlyph.Maximize));
maximize_button.Click += (o, e) => {
var form = FindForm ();

if (form != null)
form.WindowState = form.WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
if (form is not null) {
form.WindowState = form.WindowState == FormWindowState.Maximized
? FormWindowState.Normal
: FormWindowState.Maximized;

UpdateMaximizeButtonGlyph ();
}
};

close_button = Controls.AddImplicitControl (new TitleBarButton (TitleBarButton.TitleBarButtonGlyph.Close));
Expand All @@ -64,6 +69,7 @@ public bool AllowMaximize {
get => maximize_button.Visible;
set {
maximize_button.Visible = value;
UpdateMaximizeButtonGlyph ();
Invalidate (); // TODO: Shouldn't be necessary, should automatically be triggered
}
}
Expand Down Expand Up @@ -126,6 +132,8 @@ protected override void OnSizeChanged (EventArgs e)

// Keep our form image a square
form_image.Width = Height;

UpdateMaximizeButtonGlyph ();
}

/// <summary>
Expand All @@ -136,7 +144,7 @@ public bool ShowImage {
set {
if (show_image != value) {
show_image = value;
form_image.Visible = value && form_image is not null;
form_image.Visible = value && form_image.Image is not null;
Invalidate (); // TODO: Shouldn't be required
}
}
Expand All @@ -145,11 +153,38 @@ public bool ShowImage {
/// <inheritdoc/>
public override ControlStyle Style { get; } = new ControlStyle (DefaultStyle);

private void UpdateMaximizeButtonGlyph ()
{
var form = FindForm ();

if (form == null || !maximize_button.Visible) {
maximize_button.Glyph = TitleBarButton.TitleBarButtonGlyph.Maximize;
return;
}

maximize_button.Glyph = form.WindowState == FormWindowState.Maximized
? TitleBarButton.TitleBarButtonGlyph.Restore
: TitleBarButton.TitleBarButtonGlyph.Maximize;
}

internal sealed class TitleBarButton : Button
{
private const int BUTTON_PADDING = 10;

private readonly TitleBarButtonGlyph glyph;
private TitleBarButtonGlyph glyph;

/// <summary>
/// Gets or sets the glyph displayed by the button.
/// </summary>
public TitleBarButtonGlyph Glyph {
get => glyph;
set {
if (glyph != value) {
glyph = value;
Invalidate ();
}
}
}

public TitleBarButton (TitleBarButtonGlyph glyph)
{
Expand Down Expand Up @@ -183,14 +218,21 @@ protected override void OnPaint (PaintEventArgs e)
case TitleBarButtonGlyph.Maximize:
ControlPaint.DrawMaximizeGlyph (e, glyph_bounds);
break;
case TitleBarButtonGlyph.Restore:
ControlPaint.DrawRestoreGlyph (e, glyph_bounds);
break;
}
}

/// <summary>
/// Specifies which glyph is displayed by the title bar button.
/// </summary>
public enum TitleBarButtonGlyph
{
Close,
Minimize,
Maximize
Maximize,
Restore
}
}
}
Expand Down
Loading