Skip to content

Commit 3e998cb

Browse files
committed
Focus OneNote when its opened
1 parent 0c84e0e commit 3e998cb

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

Flow.Launcher.Plugin.OneNote/Flow.Launcher.Plugin.OneNote.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
1212
<UseWPF>true</UseWPF>
1313
<UseWindowsForms>true</UseWindowsForms>
14+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <!-- Required for bringing OneNote to front -->
1415
</PropertyGroup>
1516

1617
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

Flow.Launcher.Plugin.OneNote/ResultCreator.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using System.Windows.Controls;
56
using Flow.Launcher.Plugin.OneNote.Icons;
67
using Flow.Launcher.Plugin.OneNote.UI.Views;
@@ -99,6 +100,7 @@ public List<Result> EmptyQuery()
99100
Action = c =>
100101
{
101102
OneNoteApplication.CreateQuickNote(true);
103+
WindowHelper.FocusOneNote();
102104
return true;
103105
},
104106
},
@@ -120,6 +122,8 @@ public List<Result> EmptyQuery()
120122
.OrderByDescending(pg => pg.LastModified)
121123
.First()
122124
.OpenInOneNote();
125+
126+
WindowHelper.FocusOneNote();
123127
return true;
124128
},
125129
},
@@ -196,16 +200,20 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
196200
Score = score,
197201
Icon = iconProvider.GetIcon(iconInfo),
198202
ContextData = item,
199-
Action = c =>
203+
AsyncAction = async _ =>
200204
{
201205
if (actionIsAutoComplete)
202206
{
203207
context.API.ChangeQuery($"{autoCompleteText}", true);
204208
return false;
205209
}
206210

207-
item.Sync();
208-
item.OpenInOneNote();
211+
await Task.Run(() =>
212+
{
213+
item.Sync();
214+
item.OpenInOneNote();
215+
});
216+
WindowHelper.FocusOneNote();
209217
return true;
210218
},
211219
};
@@ -237,6 +245,7 @@ public Result CreateNewPageResult(string newPageName, OneNoteSection section)
237245
{
238246
OneNoteApplication.CreatePage(section, newPageName, true);
239247
Main.ForceReQuery();
248+
WindowHelper.FocusOneNote();
240249
return true;
241250
},
242251
};
@@ -273,6 +282,7 @@ public Result CreateNewSectionResult(string newSectionName, IOneNoteItem parent)
273282
}
274283

275284
Main.ForceReQuery();
285+
WindowHelper.FocusOneNote();
276286
return true;
277287
},
278288
};
@@ -309,6 +319,7 @@ public Result CreateNewSectionGroupResult(string newSectionGroupName, IOneNoteIt
309319
}
310320

311321
Main.ForceReQuery();
322+
WindowHelper.FocusOneNote();
312323
return true;
313324
},
314325
};
@@ -336,6 +347,7 @@ public Result CreateNewNotebookResult(string newNotebookName)
336347

337348
OneNoteApplication.CreateNotebook(newNotebookName, true);
338349
Main.ForceReQuery();
350+
WindowHelper.FocusOneNote();
339351
return true;
340352
},
341353
};

Flow.Launcher.Plugin.OneNote/UI/ViewModels/NewOneNotePageViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private void CreatePage(bool openImmediately)
3434
{
3535
page.OpenInOneNote();
3636
context.API.HideMainWindow();
37+
WindowHelper.FocusOneNote();
3738
}
3839
else
3940
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
6+
namespace Flow.Launcher.Plugin.OneNote
7+
{
8+
//https://stackoverflow.com/questions/2636721/bring-another-processes-window-to-foreground-when-it-has-showintaskbar-false
9+
public static partial class WindowHelper
10+
{
11+
public static void FocusOneNote()
12+
{
13+
var process = Process.GetProcessesByName("onenote").FirstOrDefault();
14+
if (process == null)
15+
return;
16+
IntPtr handle = process.MainWindowHandle;
17+
if (IsIconic(handle))
18+
{
19+
ShowWindow(handle, SW_RESTORE);
20+
}
21+
22+
SetForegroundWindow(handle);
23+
}
24+
const int SW_RESTORE = 9;
25+
26+
[LibraryImport("User32.dll")]
27+
[return: MarshalAs(UnmanagedType.Bool)]
28+
private static partial bool SetForegroundWindow(IntPtr handle);
29+
30+
[LibraryImport("User32.dll")]
31+
[return: MarshalAs(UnmanagedType.Bool)]
32+
private static partial bool ShowWindow(IntPtr handle, int nCmdShow);
33+
34+
[LibraryImport("User32.dll")]
35+
[return: MarshalAs(UnmanagedType.Bool)]
36+
private static partial bool IsIconic(IntPtr handle);
37+
}
38+
}

0 commit comments

Comments
 (0)