Skip to content

Commit

Permalink
feat: 1. Bump version up to 0.4.
Browse files Browse the repository at this point in the history
2. Add Windows Forms notify icon.
3. Use EventAggregator to trigger window state change.
  • Loading branch information
rabbitism committed Sep 29, 2020
1 parent c299f59 commit b0cb454
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ namespace GeMS_Key_Plus.Events
{
internal class EventAggregatorRepository
{
private static EventAggregator _instance;
private static EventAggregatorRepository _instance;

private EventAggregatorRepository()
{

}
public static EventAggregator GetInstance()
public static EventAggregatorRepository GetInstance()
{
if(_instance is null)
{
_instance = new EventAggregator();
_instance = new EventAggregatorRepository();
}
return _instance;
}

public EventAggregator EventAggregator { get; } = new EventAggregator();
}
}
11 changes: 11 additions & 0 deletions GeMS-Key-Plus/GeMS-Key-Plus/Events/MinimizeWindowEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace GeMS_Key_Plus.Events
{
internal class MinimizeWindowEvent:PubSubEvent
{
}
}
12 changes: 11 additions & 1 deletion GeMS-Key-Plus/GeMS-Key-Plus/GeMS-Key-Plus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>GeMS_Key_Plus</RootNamespace>
<UseWPF>true</UseWPF>
<Version>0.3.0</Version>
<UseWindowsForms>true</UseWindowsForms>
<Version>0.4.0</Version>
<Authors>Rabbitism</Authors>
<Company>Galaxism</Company>
<Product>Keyz</Product>
<ApplicationIcon>Key.ico</ApplicationIcon>
<Description>Quickly search something!</Description>
<PackageReleaseNotes>Changelog:
1. Keyz new only remains in as a notification icon, It will not show in task bar.
2. Add context menu for icon so you can right click and close Keyz.
3. Use EventAggregator to trigger window state change when click buttons with mouse.
4. Use system command to change window state instead of changing window state manually.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
<None Remove="ApplicationData.db" />
<None Remove="Key.ico" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -43,6 +50,9 @@
<Resource Include="ApplicationData.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Content Include="Key.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
26 changes: 20 additions & 6 deletions GeMS-Key-Plus/GeMS-Key-Plus/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
SizeToContent="WidthAndHeight"
AllowsTransparency="True"
FontSize="16"
d:DataContext="{x:Type vms:MainViewModel}"
d:DataContext="{x:Type vms:MaisnViewModel}"
FocusManager.FocusedElement="{Binding ElementName=buttonPanelView}"
ShowInTaskbar="False"
Title="Keyz" d:DesignHeight="450" d:DesignWidth="800">
<materialDesign:Card Margin="2" Background="White" Opacity="0.95">
<materialDesign:Card Margin="2" Background="White" Opacity="0.95" IsTabStop="False">
<StackPanel >
<materialDesign:ColorZone
Mode="PrimaryMid"
Expand Down Expand Up @@ -57,10 +59,12 @@
Command="{Binding ReloadButtonsCommand}">
<materialDesign:PackIcon Kind="Refresh" IsTabStop="False" Foreground="{StaticResource PrimaryHueMidForegroundBrush}" ></materialDesign:PackIcon>
</Button>
<Button Style="{StaticResource MaterialDesignToolForegroundButton}" IsTabStop="False"
Click="CloseButton_Click"
<Button
Style="{StaticResource MaterialDesignToolForegroundButton}"
IsTabStop="False"
Click="CloseButton_Click"
>
<materialDesign:PackIcon Kind="WindowMinimize" Foreground="{StaticResource PrimaryHueMidForegroundBrush}"></materialDesign:PackIcon>
<materialDesign:PackIcon Kind="WindowMinimize" IsTabStop="False" Foreground="{StaticResource PrimaryHueMidForegroundBrush}"></materialDesign:PackIcon>
</Button>
</StackPanel>
</Grid>
Expand All @@ -73,6 +77,7 @@
x:Name="queryStringBox"
MinWidth="400"
MaxWidth="800"
MaxHeight="400"
Margin="8 0"
AcceptsReturn="True"
FontSize="20"
Expand All @@ -82,6 +87,7 @@
Foreground="{StaticResource PrimaryHueMidBrush}"
materialDesign:TextFieldAssist.HasClearButton="True"
Text="{Binding QueryString, Mode=TwoWay}"
TabIndex="2"
>

</TextBox>
Expand Down Expand Up @@ -153,7 +159,15 @@
</Grid>
</StackPanel>
</materialDesign:ColorZone>
<views:ButtonPanelView x:Name="buttonPanelView" DataContext="{Binding ButtonPanel}" PreviewKeyUp="ButtonPanelView_KeyUp" Margin="16 8"></views:ButtonPanelView>
<views:ButtonPanelView
x:Name="buttonPanelView"
DataContext="{Binding ButtonPanel}"
KeyUp="ButtonPanelView_KeyUp"
Margin="16 8"
TabIndex="1"
>

</views:ButtonPanelView>
</StackPanel>


Expand Down
130 changes: 101 additions & 29 deletions GeMS-Key-Plus/GeMS-Key-Plus/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
using AutoHotkey.Interop;
using GeMS_Key_Plus.Global;
using GeMS_Key_Plus.Events;
using GeMS_Key_Plus.Models;
using GeMS_Key_Plus.ViewModels;
using GeMS_Key_Plus.Views;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GeMS_Key_Plus
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public partial class MainWindow : Window, IDisposable
{
private HotKey _hotKey;
private AutoHotkeyEngine Ahk;
private AutoHotkeyEngine _ahk;
private System.Windows.Forms.NotifyIcon _icon;
private bool disposedValue;

public MainWindow()
{
InitializeComponent();
InitializeNotifyIcon();
this.DataContext = new MainViewModel();
_hotKey = new HotKey(Key.G, KeyModifier.Alt, Copy);
this.WindowState = WindowState.Minimized;
EventAggregatorRepository
.GetInstance()
.EventAggregator
.GetEvent<MinimizeWindowEvent>()
.Subscribe(OnMinimizeWindowEventRaised);
}

private void InitializeNotifyIcon()
{
_icon ??= new System.Windows.Forms.NotifyIcon();
_icon.Icon = new System.Drawing.Icon("Key.ico");
_icon.MouseClick += ShowWindow;
_icon.Visible = true;
_icon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();

_icon.ContextMenuStrip.Items.AddRange(
new System.Windows.Forms.ToolStripItem[]
{
new System.Windows.Forms.ToolStripLabel("Keyz"),
new System.Windows.Forms.ToolStripSeparator(),
new System.Windows.Forms.ToolStripMenuItem("Close", null, (o, e) =>
{
this.Dispose(disposedValue);
this.Close();
}),
}

);

}

private void ColorZone_MouseDown(object sender, MouseButtonEventArgs e)
Expand All @@ -48,40 +68,46 @@ private void ColorZone_MouseDown(object sender, MouseButtonEventArgs e)

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
// this.WindowState = WindowState.Minimized;
SystemCommands.MinimizeWindow(this);
Hide();
}

private void Copy(HotKey hotkey)
{
if (hotkey == null) throw new ArgumentNullException(nameof(hotkey));
Ahk = AutoHotkeyEngine.Instance;
_ahk = AutoHotkeyEngine.Instance;
if (WindowState == WindowState.Minimized)
{
Ahk.ExecRaw("Send, ^c");
_ahk.ExecRaw("Send, ^c");
Thread.Sleep(200);

(this.DataContext as MainViewModel).QueryString = Clipboard.GetText().Trim();
WindowState = WindowState.Normal;

if (this.DataContext is MainViewModel vm)
{
vm.QueryString = Clipboard.GetText().Trim();
}
SystemCommands.RestoreWindow(this);
Show();
Activate();
Keyboard.Focus(buttonPanelView);
ShowInTaskbar = true;
// Keyboard.Focus(buttonPanelView);
buttonPanelView.Focus();
FocusManager.SetFocusedElement(this, buttonPanelView);
}
else
{
WindowState = WindowState.Minimized;
ShowInTaskbar = true;
OnMinimizeWindowEventRaised();
}
}

protected override void OnClosing(CancelEventArgs e)
{
_hotKey.Dispose();
_hotKey?.Dispose();
base.OnClosing(e);
}

private void ButtonPanelView_KeyUp(object sender, KeyEventArgs e)
{
if(this.DataContext is MainViewModel vm)
if (this.DataContext is MainViewModel vm)
{
vm.Query(e);
}
Expand All @@ -93,6 +119,52 @@ private void SettingButton_Click(object sender, RoutedEventArgs e)
settingWindow.Show();
}


private void ShowWindow(object sender, System.Windows.Forms.MouseEventArgs args)
{
if (args.Button == System.Windows.Forms.MouseButtons.Left)
{
SystemCommands.RestoreWindow(this);
Show();
Activate();
}

}

private void OnMinimizeWindowEventRaised()
{
SystemCommands.MinimizeWindow(this);
Hide();
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
_hotKey?.Dispose();
_icon?.Dispose();
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}

// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~MainWindow()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }

void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
9 changes: 7 additions & 2 deletions GeMS-Key-Plus/GeMS-Key-Plus/ViewModels/LinkButtonViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GeMS_Key_Plus.Global;
using GeMS_Key_Plus.Events;
using GeMS_Key_Plus.Global;
using GeMS_Key_Plus.Models;
using Prism.Commands;
using Prism.Mvvm;
Expand Down Expand Up @@ -109,7 +110,11 @@ public void Query()
Process.Start(psi);
}
GlobalVariables.ActionHistory.Put(GlobalVariables.QueryString, HotKey);
App.Current.MainWindow.WindowState = System.Windows.WindowState.Minimized;
EventAggregatorRepository
.GetInstance()
.EventAggregator
.GetEvent<MinimizeWindowEvent>()
.Publish();
}

private void MultipleQuery()
Expand Down
1 change: 1 addition & 0 deletions GeMS-Key-Plus/GeMS-Key-Plus/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public MainViewModel()
ClearCacheCommand = new DelegateCommand(ClearCache);
EventAggregatorRepository
.GetInstance()
.EventAggregator
.GetEvent<RefreshButtonLayoutEvent>()
.Subscribe(ReloadButtons);
}
Expand Down
6 changes: 5 additions & 1 deletion GeMS-Key-Plus/GeMS-Key-Plus/Views/Setting.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public Setting()

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
EventAggregatorRepository.GetInstance().GetEvent<RefreshButtonLayoutEvent>().Publish();
EventAggregatorRepository
.GetInstance()
.EventAggregator
.GetEvent<RefreshButtonLayoutEvent>()
.Publish();
this.Close();
}

Expand Down

0 comments on commit b0cb454

Please sign in to comment.