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

Material Scrim #275

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion MaterialSkin/Controls/FlexibleMaterialDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,12 @@ public static DialogResult Show(IWin32Window owner, string text, string caption,
//Moved after SetDialogSizes() because it needs Dialog.Width property set.
SetDialogButtons(FlexibleMaterialForm, buttons, defaultButton, buttonsPosition);
//Show the dialog
return FlexibleMaterialForm.ShowDialog(owner);
var form = owner as Form;

form?.ShowScrim();
var dialogResult = FlexibleMaterialForm.ShowDialog(owner);
form?.HideScrim();
return dialogResult;
}

private void FlexibleMaterialForm_Load(object sender, EventArgs e)
Expand Down
26 changes: 5 additions & 21 deletions MaterialSkin/Controls/MaterialForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private bool Maximized
private Point _animationSource;
private Padding originalPadding;

private Form drawerOverlay = new Form();
private DrawerScrim drawerOverlay;
private Form drawerForm = new Form();

// Drawer overlay and speed improvements
Expand Down Expand Up @@ -458,30 +458,17 @@ protected void AddDrawerOverlayForm()
Increment = 0.04
};

// Overlay Form definitions
drawerOverlay = new DrawerScrim(this);

_drawerShowHideAnimManager.OnAnimationProgress += (sender) =>
{
drawerOverlay.Opacity = (float)(_drawerShowHideAnimManager.GetProgress() * 0.55f);
drawerOverlay.Opacity = _drawerShowHideAnimManager.GetProgress() * 0.55;
};

int H = ClientSize.Height - _statusBarBounds.Height - _actionBarBounds.Height;
int Y = PointToScreen(Point.Empty).Y + _statusBarBounds.Height + _actionBarBounds.Height;

// Overlay Form definitions
drawerOverlay.BackColor = Color.Black;
drawerOverlay.Opacity = 0;
drawerOverlay.MinimizeBox = false;
drawerOverlay.MaximizeBox = false;
drawerOverlay.Text = "";
drawerOverlay.ShowIcon = false;
drawerOverlay.ControlBox = false;
drawerOverlay.FormBorderStyle = FormBorderStyle.None;
drawerOverlay.Visible = true;
drawerOverlay.Size = new Size(ClientSize.Width, H);
drawerOverlay.Location = new Point(PointToScreen(Point.Empty).X, Y);
drawerOverlay.ShowInTaskbar = false;
drawerOverlay.Owner = this;
drawerOverlay.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

// Drawer Form definitions
drawerForm.BackColor = Color.LimeGreen;
drawerForm.TransparencyKey = Color.LimeGreen;
Expand Down Expand Up @@ -536,14 +523,12 @@ protected void AddDrawerOverlayForm()
{
H = ClientSize.Height - _statusBarBounds.Height - _actionBarBounds.Height;
drawerForm.Size = new Size(DrawerWidth, H);
drawerOverlay.Size = new Size(ClientSize.Width, H);
};

Move += (sender, e) =>
{
Point pos = new Point(PointToScreen(Point.Empty).X, PointToScreen(Point.Empty).Y + _statusBarBounds.Height + _actionBarBounds.Height);
drawerForm.Location = pos;
drawerOverlay.Location = pos;
};

// Close when click outside menu
Expand Down Expand Up @@ -597,7 +582,6 @@ protected void AddDrawerOverlayForm()
FixFormPadding(this);

// Fix Closing the Drawer or Overlay form with Alt+F4 not exiting the app
drawerOverlay.FormClosed += TerminateOnClose;
drawerForm.FormClosed += TerminateOnClose;
}

Expand Down
109 changes: 109 additions & 0 deletions MaterialSkin/Controls/MaterialScrim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace MaterialSkin.Controls
{
public class MaterialScrim : Form
{
public new double Opacity
{
get => base.Opacity;
set
{
if (value == base.Opacity) return;

Visible = value != 0;
base.Opacity = value;
}
}

public MaterialScrim(Form owner)
{
InitializeComponent();

Owner = owner ?? throw new ArgumentNullException(nameof(owner));
Size = Owner.Size;
Location = Owner.Location;

Owner.Resize += (sender, e) => OnResize(e);
Owner.Move += (sender, e) => OnMove(e);
Owner.FormClosed += (sender, e) => OnFormClosed(e);
Owner.Disposed += (sender, e) => Dispose();
Owner.VisibleChanged += (sender, e) => OnVisibleChanged(e);
}

private void InitializeComponent()
{
BackColor = Color.Black;
Opacity = 0;
MinimizeBox = false;
MaximizeBox = false;
ShowIcon = false;
ControlBox = false;
Text = string.Empty;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
}

public new void Show() => Opacity = 0.55;

public new void Hide() => Opacity = 0;

protected override void OnResize(EventArgs e)
{
if (Owner == null) return;
Size = Owner.Size;
}

protected override void OnMove(EventArgs e)
{
if (Owner == null) return;
Location = Owner.Location;
}
}

public static class ScrimExtensions
{
public static void ShowScrim(this Form form) => GetScrim(form).Show();

public static void HideScrim(this Form form) => GetScrim(form).Hide();

private static MaterialScrim GetScrim(Form form)
{
var scrims = form.OwnedForms.Where(f => f is MaterialScrim);
MaterialScrim scrim;

if (!scrims.Any())
scrim = new MaterialScrim(form);
else
scrim = scrims.First() as MaterialScrim;

return scrim;
}
}

public class DrawerScrim : MaterialScrim
{
public DrawerScrim(MaterialForm owner) : base(owner)
{
var rect = owner.RectangleToScreen(owner.UserArea);

Size = rect.Size;
Location = rect.Location;
}

protected override void OnResize(EventArgs e)
{
if (Owner == null) return;
Size = (Owner as MaterialForm).UserArea.Size;
}

protected override void OnMove(EventArgs e)
{
if (Owner == null) return;
Location = Owner.RectangleToScreen((Owner as MaterialForm).UserArea).Location;
}
}
}
3 changes: 3 additions & 0 deletions MaterialSkin/MaterialSkin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<Compile Include="Controls\MaterialListBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\MaterialScrim.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Controls\MaterialScrollBar.cs">
<SubType>Component</SubType>
</Compile>
Expand Down