Skip to content

Commit

Permalink
fix auto dock setting not getting saved;
Browse files Browse the repository at this point in the history
change app reopen action to undock only;
add tray icon and menu (WIP);
update wpf toolkit;
  • Loading branch information
Carl Chang committed Oct 22, 2017
1 parent 6115aa7 commit 3d72567
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 30 deletions.
25 changes: 14 additions & 11 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public partial class App : Application
//internal const int MaxWindowCount = 2;//need to set this to 4 while debugging if you use live debug toolbar in vs2015.
public const string MutexString = @"{39622D35-176E-453D-B1FD-E4EC1EAF31DC}";
private System.Threading.Mutex mtx;
public static Hardcodet.Wpf.TaskbarNotification.TaskbarIcon TrayIcon;

[DllImport("shlwapi.dll")]
private static extern bool PathIsNetworkPath(string pszPath);
Expand All @@ -35,18 +36,20 @@ private void RunCheck(object sender1, StartupEventArgs e1)
AppDomain.CurrentDomain.AssemblyResolve += (object sender, ResolveEventArgs e) =>
{
var desiredAssembly = new System.Reflection.AssemblyName(e.Name).Name;
if (desiredAssembly == "Xceed.Wpf.Toolkit")
switch (desiredAssembly)
{
var ressourceName = "DesktopNote.Resources." + desiredAssembly + ".dll";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return System.Reflection.Assembly.Load(assemblyData);
}
case "Xceed.Wpf.Toolkit":
case "Hardcodet.Wpf.TaskbarNotification":
var ressourceName = "DesktopNote.Resources." + desiredAssembly + ".dll";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return System.Reflection.Assembly.Load(assemblyData);
}
default:
return null;
}
else
return null;
};

//localization
Expand Down Expand Up @@ -112,7 +115,7 @@ private void RunCheck(object sender1, StartupEventArgs e1)
}

if (MainWindows.Count == 0) Win_Format.NewNote();
}
}

public static void Quit(bool savesetting)
{
Expand Down
10 changes: 10 additions & 0 deletions DesktopNote.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Hardcodet.Wpf.TaskbarNotification, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Resources\Hardcodet.Wpf.TaskbarNotification.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
Expand All @@ -82,6 +87,7 @@
</ApplicationDefinition>
<Compile Include="Setting.cs" />
<Compile Include="SingleInstance.cs" />
<Compile Include="WinAPI.cs" />
<Compile Include="Win_Format.xaml.cs">
<DependentUpon>Win_Format.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -147,6 +153,7 @@
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down Expand Up @@ -198,6 +205,9 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Hardcodet.Wpf.TaskbarNotification.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
43 changes: 31 additions & 12 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Hardcodet.Wpf.TaskbarNotification;
using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
Expand Down Expand Up @@ -43,7 +44,7 @@ public DockStatus DockedTo
private static void OnDockedToChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ds = (DockStatus)e.NewValue;
var win = (Window)d;
var win = (MainWindow)d;
if (ds == DockStatus.None)
{
win.ResizeMode = ResizeMode.CanResizeWithGrip;
Expand Down Expand Up @@ -140,7 +141,7 @@ internal void DockToSide(bool changpos = false)
}
}

private void UnDock()
internal void UnDock()
{
if (DockedTo != DockStatus.Docking && DockedTo != DockStatus.None)
{
Expand Down Expand Up @@ -182,6 +183,7 @@ private void UnDock()
var anim_prop = new ObjectAnimationUsingKeyFrames();
anim_prop.KeyFrames.Add(new DiscreteObjectKeyFrame(DockStatus.Docking, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0))));
anim_prop.KeyFrames.Add(new DiscreteObjectKeyFrame(dockto, KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 500))));

BeginAnimation(tgtpro, anim_move);
BeginAnimation(OpacityProperty, anim_fade);
BeginAnimation(DockedToProperty, anim_prop);
Expand Down Expand Up @@ -251,9 +253,10 @@ private void RTB_Main_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
e.Handled = true;
//update caller
App.FormatWindow.MainWin = this;
App.FormatWindow.RTB_Main = RTB_Main;
App.FormatWindow.UpdateCaller(this, this);
App.FormatWindow.FadeIn();
if (App.FormatWindow.Opacity == 1)//refresh values manually when the window is already visible.
App.FormatWindow.LoadValues();
}

private void RTB_Main_PreviewMouseDown(object sender, MouseButtonEventArgs e)
Expand Down Expand Up @@ -373,6 +376,21 @@ internal void SaveToXamlPkg()

private void Win_Main_Loaded(object sender, RoutedEventArgs e)
{
//WinAPI.SetToolWindow(this);

//create tray icon
if (App.TrayIcon == null)
{
using (var stream = Application.GetResourceStream(new Uri("pack://application:,,,/DesktopNote;component/Resources/stickynote.ico")).Stream)
{
App.TrayIcon = new TaskbarIcon
{
Icon = new System.Drawing.Icon(stream),
ToolTipText = nameof(DesktopNote)
};
}
}

//check and merge previous settings
if (Properties.Settings.Default.UpgradeFlag == true)
{
Expand All @@ -388,6 +406,7 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e)
Top = CurrentSetting.Win_Pos.Y;

lastdockstatus = (DockStatus)CurrentSetting.DockedTo;
DockedTo = lastdockstatus;
RTB_Main.FontFamily = new FontFamily(CurrentSetting.Font);
RTB_Main.Foreground = new SolidColorBrush(CurrentSetting.FontColor);
RTB_Main.Background = new SolidColorBrush(CurrentSetting.BackColor);
Expand Down Expand Up @@ -474,6 +493,7 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e)

var source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
source.AddHook(WndProc);

}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
Expand All @@ -483,16 +503,15 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
//activate and reset dock status when a second instance is requested
//in case of resolution changes etc that might cause the main window to be "lost".
Activate();
App.CurrScrnRect = new GetCurrentMonitor().GetInfo(this);
BeginAnimation(DockedToProperty, null); //required to modify the DockedTo property.
Top = App.CurrScrnRect.Top; //in case it was docked to bottom or top.
lastdockstatus = DockStatus.Left;
DockedTo = DockStatus.Left;
//App.CurrScrnRect = new GetCurrentMonitor().GetInfo(this);
//BeginAnimation(DockedToProperty, null); //required to modify the DockedTo property.
//Top = App.CurrScrnRect.Top; //in case it was docked to bottom or top.
//lastdockstatus = DockStatus.Left;
//DockedTo = DockStatus.Left;
//resetting docking position was moved to tray menu.
UnDock();
}
return IntPtr.Zero;
}


}
}
Binary file not shown.
Binary file modified Resources/Xceed.Wpf.Toolkit.dll
Binary file not shown.
Binary file added Resources/Xceed.Wpf.Toolkit.dll.old
Binary file not shown.
4 changes: 3 additions & 1 deletion RoundedWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public override void OnApplyTemplate()
PreviewGotKeyboardFocus += (object sender, KeyboardFocusChangedEventArgs e) =>
{
e.Handled = true;
Owner.Focus();
var o = sender as Window;
if (o != null && o != this) o.Focus();
};
}
}
Expand Down Expand Up @@ -243,6 +244,7 @@ protected override void OnClosing(CancelEventArgs e)

/// <summary>
/// Fade in the window at mouse position. Or the specified Left and Top position.
/// If the window is already faded in, move the window to the new position. In this case the FadingInEvent will not be raised.
/// </summary>
public void FadeIn(double left = double.NaN, double top = double.NaN)
{
Expand Down
2 changes: 1 addition & 1 deletion Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public bool AutoDock
}
set
{
set.Bak_Location[SettingIndex] = value.ToString();
set.AutoDock[SettingIndex] = value.ToString();
}
}
public int DockedTo
Expand Down
32 changes: 32 additions & 0 deletions WinAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace DesktopNote
{
class WinAPI
{
#region ToolWindow
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr window, int index, int value);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr window, int index);

const int GWL_EXSTYLE = -20;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_APPWINDOW = 0x00040000;

public static void SetToolWindow(Window win)
{
var handle = new WindowInteropHelper(win).Handle;
var a = SetWindowLong(handle, GWL_EXSTYLE, GetWindowLong(handle, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
}
#endregion

}
}
2 changes: 1 addition & 1 deletion Win_Format.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>

<wtk:ColorPicker x:Key="CP_Main" x:Shared="False" Width="22" ColorMode="ColorCanvas" ShowAdvancedButton="False"
<wtk:ColorPicker x:Key="CP_Main" x:Shared="False" Width="22" ColorMode="ColorCanvas"
ShowRecentColors="True" SelectedColorChanged="ColorChange" SelectedColor="Black" ShowDropDownButton="False"/>
</Grid.Resources>

Expand Down
17 changes: 15 additions & 2 deletions Win_Format.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ public partial class Win_Format : RoundedWindow
public Win_Format(Window owner, MainWindow mainwin)
{
InitializeComponent();
UpdateCaller(owner, mainwin);
}

public void UpdateCaller(Window owner, MainWindow mainwin)
{
if (owner != null) Owner = owner;
MainWin = mainwin;
RTB_Main = MainWin.RTB_Main;
RTB_Main = mainwin.RTB_Main;
}

#region Menu Events
Expand Down Expand Up @@ -191,7 +196,15 @@ private void Button_Options_Click(object sender, RoutedEventArgs e)

private void FB1_FadingIn(object sender, RoutedEventArgs e)
{
//loading formats
//initialize values
LoadValues();
}

/// <summary>
/// Manually refresh control values.
/// </summary>
internal void LoadValues()
{
if (!RTB_Main.Selection.IsEmpty)
{
var caretfont = RTB_Main.Selection.GetPropertyValue(TextElement.FontFamilyProperty) as FontFamily;
Expand Down
2 changes: 1 addition & 1 deletion Win_Options.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<TextBlock Grid.Row="1" Grid.Column="0" Text="{StaticResource label_content_path_txt}" VerticalAlignment="Center" Margin="5,2"/>
<TextBox Grid.Row="1" Grid.Column="1" x:Name="TB_SavePathTxt" VerticalAlignment="Center" Margin="5,2" IsReadOnly="True" PreviewMouseDoubleClick="TB_SavePathTxt_PreviewMouseDoubleClick"/>
<StackPanel Orientation="Horizontal" Grid.Row="2" Margin="5,2">
<wtk:ColorPicker x:Name="CP_Paper" Margin="0,0,5,0" Width="22" ColorMode="ColorCanvas" ShowAdvancedButton="False"
<wtk:ColorPicker x:Name="CP_Paper" Margin="0,0,5,0" Width="22" ColorMode="ColorCanvas"
ShowRecentColors="True" SelectedColorChanged="ColorChange" SelectedColor="Black" ShowDropDownButton="False"/>
<TextBlock Text="{StaticResource menu_paper_color}" VerticalAlignment="Center"/>
</StackPanel>
Expand Down
3 changes: 2 additions & 1 deletion Win_Options.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private void CB_AutoDock_Click(object sender, RoutedEventArgs e)
else
{
MainWin.CurrentSetting.AutoDock = false;
MainWin.Topmost = false;
MainWin.UnDock();
//MainWin.Topmost = false;
}
MainWin.CurrentSetting.Save();
}
Expand Down

0 comments on commit 3d72567

Please sign in to comment.