Skip to content

Commit

Permalink
Revert three commits of #26 Get rid of ApplicationWindow forms: f8c42fb
Browse files Browse the repository at this point in the history
…, 386d55f, and 81d1cd2.
  • Loading branch information
FrigoCoder committed Mar 18, 2018
1 parent 81d1cd2 commit 4332e82
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 86 deletions.
29 changes: 9 additions & 20 deletions FrigoTab/ApplicationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,41 @@

namespace FrigoTab {

public class ApplicationWindow : IDisposable {
public class ApplicationWindow : FrigoForm {

public readonly Rectangle Bounds;
public readonly WindowHandle Application;
public Property<bool> Selected;
private readonly int index;
private readonly LayerUpdater layerUpdater;
private readonly Thumbnail thumbnail;
private readonly WindowIcon windowIcon;
private bool disposed;

public ApplicationWindow (FrigoForm owner, WindowHandle application, int index, Rectangle bounds, LayerUpdater layerUpdater) {
Bounds = bounds.ScreenToClient(owner.WindowHandle);
public ApplicationWindow (FrigoForm owner, WindowHandle application, int index, Rectangle bounds) {
Bounds = bounds;
Owner = owner;
ExStyle |= WindowExStyles.Transparent | WindowExStyles.Layered;
Application = application;
Selected.Changed += (x, y) => RenderOverlay();
this.index = index;
this.layerUpdater = layerUpdater;
thumbnail = new Thumbnail(application, owner.WindowHandle);
thumbnail.SetDestinationRect(new Rect(Bounds));
thumbnail.SetDestinationRect(new Rect(Bounds).ScreenToClient(owner.WindowHandle));
windowIcon = new WindowIcon(application);
windowIcon.Changed += RenderOverlay;
RenderOverlay();
}

~ApplicationWindow () => Dispose();

public void Dispose () {
if( disposed ) {
return;
}
protected override void Dispose (bool disposing) {
thumbnail.Dispose();
disposed = true;
GC.SuppressFinalize(this);
base.Dispose(disposing);
}

private void RenderOverlay () => layerUpdater.Update(RenderOverlay);
private void RenderOverlay () => LayerUpdater.Update(this, RenderOverlay);

private void RenderOverlay (Graphics graphics) {
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.SetClip(Bounds);
graphics.Clear(Color.Empty);
RenderFrame(graphics);
RenderTitle(graphics);
RenderNumber(graphics);
graphics.ResetClip();
}

private void RenderFrame (Graphics graphics) {
Expand Down
32 changes: 16 additions & 16 deletions FrigoTab/ApplicationWindows.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace FrigoTab {

public class ApplicationWindows : FrigoForm {
public class ApplicationWindows : IDisposable {

public Property<ApplicationWindow> Selected;
public readonly LayerUpdater LayerUpdater;
public Property<bool> Visible;
private readonly IList<ApplicationWindow> windows = new List<ApplicationWindow>();

public ApplicationWindows (FrigoForm owner, WindowFinder finder) {
Owner = owner;
Bounds = owner.Bounds;
ExStyle |= WindowExStyles.Transparent | WindowExStyles.Layered;
Selected.Changed += (oldWindow, newWindow) => {
oldWindow?.Selected.Set(false);
newWindow?.Selected.Set(true);
};
LayerUpdater = new LayerUpdater(this);
Visible.Changed += (oldValue, value) => {
foreach( ApplicationWindow window in windows ) {
window.Visible = value;
}
};
Layout layout = new Layout(finder.Windows);
foreach( WindowHandle handle in finder.Windows ) {
windows.Add(new ApplicationWindow(owner, handle, windows.Count, layout.Bounds[handle], LayerUpdater));
windows.Add(new ApplicationWindow(owner, handle, windows.Count, layout.Bounds[handle]));
}
}

public void SelectByIndex (int index) => Selected.Value = index >= 0 && index < windows.Count ? windows[index] : null;

public void SelectByPoint (Point point) =>
Selected.Value = windows.FirstOrDefault(window => window.Bounds.Contains(point.ScreenToClient(WindowHandle)));
~ApplicationWindows () => Dispose();

protected override void Dispose (bool disposing) {
public void Dispose () {
foreach( ApplicationWindow window in windows ) {
window.Dispose();
window.Close();
}
windows.Clear();
LayerUpdater.Dispose();
base.Dispose(disposing);
}

public void SelectByIndex (int index) => Selected.Value = index >= 0 && index < windows.Count ? windows[index] : null;
public void SelectByPoint (Point point) => Selected.Value = windows.FirstOrDefault(window => window.Bounds.Contains(point));

}

}
69 changes: 24 additions & 45 deletions FrigoTab/LayerUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,26 @@

namespace FrigoTab {

public class LayerUpdater : IDisposable {
public static class LayerUpdater {

public delegate void Renderer (Graphics graphics);

private readonly Form form;
private readonly IntPtr screenDc;
private readonly IntPtr memDc;
private readonly IntPtr hBitmap;
private readonly IntPtr hOldBitmap;
private bool disposed;

public LayerUpdater (Form form) {
this.form = form;
screenDc = GetDC(IntPtr.Zero);
memDc = CreateCompatibleDC(screenDc);
hBitmap = CreateCompatibleBitmap(screenDc, form.Bounds.Width, form.Bounds.Height);
hOldBitmap = SelectObject(memDc, hBitmap);
}

~LayerUpdater () => Dispose();

public void Dispose () {
if( disposed ) {
return;
}
SelectObject(memDc, hOldBitmap);
DeleteDC(memDc);
DeleteObject(hBitmap);
ReleaseDC(IntPtr.Zero, screenDc);
disposed = true;
GC.SuppressFinalize(this);
}

public void Update (Renderer renderer) {
public static void Update (Form form, Renderer renderer) {
if( form.IsDisposed ) {
return;
}
IntPtr screenDc = GetDC(IntPtr.Zero);
IntPtr memDc = CreateCompatibleDC(screenDc);
IntPtr hBitmap = CreateCompatibleBitmap(screenDc, form.Bounds.Width, form.Bounds.Height);
IntPtr hOldBitmap = SelectObject(memDc, hBitmap);
using( Graphics graphics = Graphics.FromHdc(memDc) ) {
renderer(graphics);
}
UpdateLayeredWindow();
}

private void UpdateLayeredWindow () {
Point pptDst = form.Bounds.Location;
Size pSize = form.Bounds.Size;
Point pptSrc = Point.Empty;
BlendFunction pblend = new BlendFunction {
BlendOperation = BlendOperation.SourceOver,
BlendFlags = 0,
SourceConstantAlpha = 0xff,
AlphaFormat = AlphaFormat.SourceAlpha
};
UpdateLayeredWindow(form.Handle, IntPtr.Zero, ref pptDst, ref pSize, memDc, ref pptSrc, 0, ref pblend, UpdateLayeredWindowFlags.Alpha);
UpdateLayeredWindow(form, memDc);
SelectObject(memDc, hOldBitmap);
DeleteDC(memDc);
DeleteObject(hBitmap);
ReleaseDC(IntPtr.Zero, screenDc);
}

private struct BlendFunction {
Expand Down Expand Up @@ -91,6 +57,19 @@ private enum UpdateLayeredWindowFlags {

}

private static void UpdateLayeredWindow (Control form, IntPtr hdc) {
Point pptDst = form.Bounds.Location;
Size pSize = form.Bounds.Size;
Point pptSrc = Point.Empty;
BlendFunction pblend = new BlendFunction {
BlendOperation = BlendOperation.SourceOver,
BlendFlags = 0,
SourceConstantAlpha = 0xff,
AlphaFormat = AlphaFormat.SourceAlpha
};
UpdateLayeredWindow(form.Handle, IntPtr.Zero, ref pptDst, ref pSize, hdc, ref pptSrc, 0, ref pblend, UpdateLayeredWindowFlags.Alpha);
}

[DllImport("user32.dll")]
private static extern IntPtr GetDC (IntPtr hwnd);

Expand Down
3 changes: 0 additions & 3 deletions FrigoTab/Points.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public static Point ScreenToClient (this Point point, WindowHandle handle) {
return point;
}

public static Rectangle ScreenToClient (this Rectangle rectangle, WindowHandle handle) =>
new Rectangle(rectangle.Location.ScreenToClient(handle), rectangle.Size);

[DllImport("user32.dll")]
private static extern bool ClientToScreen (WindowHandle hWnd, ref Point lpPoint);

Expand Down
6 changes: 4 additions & 2 deletions FrigoTab/SessionForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ private void BeginSession () {
backgrounds = new BackgroundWindows(this, finder);
applications = new ApplicationWindows(this, finder);

applications.SelectByIndex(0);

Visible = true;
applications.Visible = true;
applications.Visible.Value = true;
WindowHandle.SetForeground();

active = true;
Expand All @@ -69,7 +71,7 @@ private void EndSession () {
}
active = false;

applications.Visible = false;
applications.Visible.Value = false;
Visible = false;

applications.Dispose();
Expand Down

0 comments on commit 4332e82

Please sign in to comment.