Skip to content
Merged
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
23 changes: 23 additions & 0 deletions Win32/AppIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ namespace Bemo
/// </summary>
public static class AppIcons
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct StockIconInfo
{
public uint Size;
public IntPtr Icon;
public int SystemIndex, ResourceIndex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string Path;
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern int SHGetStockIconInfo(uint id, uint flags, ref StockIconInfo info);

// The caller owns the copy; release the native shell handle immediately.
public static Icon GetStockIcon(uint id)
{
var info = new StockIconInfo { Size = (uint)Marshal.SizeOf(typeof(StockIconInfo)) };
try
{
if (SHGetStockIconInfo(id, 0x100, ref info) < 0 || info.Icon == IntPtr.Zero) return null;
return (Icon)Icon.FromHandle(info.Icon).Clone();
}
finally { if (info.Icon != IntPtr.Zero) WinUserApi.DestroyIcon(info.Icon); }
}

private delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
Expand Down
29 changes: 25 additions & 4 deletions WtProgram/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ module Commands =
let current = Services.settings.getValue key :?> bool
Services.settings.setValue(key,box (not current)))
with listed=false }
let private iconChoices hwnd title restore current apply =
let pick (path,label) = choice label (if current=Some path then tr Strings.TabMenu.currentIcon else "") (fun () -> apply (Some path))
let section label entries = { choice (tr label) "" ignore with concealed=false; run=fun () -> Choose(List.map pick entries) }
[ section Strings.TabMenu.systemTabIcons (TabIconChoices.windows |> List.map(fun (path,label) -> path,tr label))
section Strings.TabMenu.commonTabIcons (TabIconChoices.common |> List.map(fun (path,label,_,_) -> path,tr label))
let applications = TabIconChoices.applications()
if not applications.IsEmpty then section Strings.TabMenu.appTabIcons applications
let recent = TabIconChoices.recent Services.settings.root |> List.filter IO.File.Exists |> List.map(fun path -> path,IO.Path.GetFileName path)
if not recent.IsEmpty then section Strings.TabMenu.recentTabIcons recent
choice (tr Strings.TabMenu.chooseTabIconFile) "" (fun () ->
TabIconPicker.chooseFor title {new System.Windows.Forms.IWin32Window with member _.Handle=hwnd}
apply)
choice (tr restore) "" (fun () -> apply None) ]

let all : Command list = [
fromCatalog "next-tab" TabCommands hasOtherTabs (onGroup(fun g -> g.switchWindow(true,false)))
fromCatalog "previous-tab" TabCommands hasOtherTabs (onGroup(fun g -> g.switchWindow(false,false)))
Expand Down Expand Up @@ -469,6 +483,14 @@ module Commands =
if canRememberGroupColor hwnd then
yield choice (tr Strings.Settings.rememberGroupColor) (if (Services.program.getGroupColor hwnd).IsSome then "✓" else "") (fun () -> rememberGroupColor hwnd)
yield choice (tr Strings.Settings.clearTabColor) "" (fun () -> clearTabColor hwnd) ])
choosing "tab-icon" Strings.Commands.tabIcon TabCommands hasGroup (fun hwnd ->
iconChoices hwnd Strings.TabMenu.tabIcon Strings.TabMenu.restoreTabIcon (Services.program.getTabIcon hwnd)
(fun path -> onTab (fun g hwnd -> g.run(SetTabIcon(hwnd,path))) hwnd))
plain "change-tab-icon" Strings.Commands.changeTabIcon TabCommands hasGroup (onTab(fun group hwnd ->
TabIconPicker.choose {new System.Windows.Forms.IWin32Window with member _.Handle=hwnd}
(fun path -> group.run(SetTabIcon(hwnd,path)))))
plain "restore-tab-icon" Strings.Commands.restoreTabIcon TabCommands (fun hwnd -> hasGroup hwnd && (Services.program.getTabIcon hwnd).IsSome)
(onTab (fun g hwnd -> g.run(SetTabIcon(hwnd,None))))
plain "custom-tab-color" Strings.Commands.customTabColor TabCommands hasGroup customTabColor
plain "remember-tab-color" Strings.Commands.rememberTabColor TabCommands canRememberTabColor rememberTabColor
plain "remember-group-color" Strings.Commands.rememberGroupColor TabCommands canRememberGroupColor rememberGroupColor
Expand Down Expand Up @@ -589,10 +611,9 @@ module Commands =
| _ -> ()))
with state=fun hwnd -> if hasGroup hwnd then Some(combinesIcons hwnd) else None }
// Only a group with combined icons has a taskbar button of its own to change.
plain "change-group-icon" Strings.Commands.changeGroupIcon GroupCommands combinesIcons (fun hwnd ->
use dialog = new System.Windows.Forms.OpenFileDialog(Title=tr Strings.TabMenu.changeTaskbarIcon,
Filter=Localization.plain Strings.TabMenu.taskbarIconFiles,CheckFileExists=true)
if dialog.ShowDialog()=System.Windows.Forms.DialogResult.OK then Services.program.setGroupIcon(hwnd,Some dialog.FileName))
choosing "change-group-icon" Strings.Commands.changeGroupIcon GroupCommands combinesIcons (fun hwnd ->
iconChoices hwnd Strings.TabMenu.taskbarIcon Strings.TabMenu.restoreTaskbarIcon (Services.program.groupIcon hwnd)
(fun path -> Services.program.setGroupIcon(hwnd,path)))
plain "restore-group-icon" Strings.Commands.restoreGroupIcon GroupCommands combinesIcons (fun hwnd -> Services.program.setGroupIcon(hwnd,None))
// The app's own switches: each shows whether it is on for the app in front, with or
// without tabs, so their keys are held everywhere.
Expand Down
34 changes: 0 additions & 34 deletions WtProgram/GroupPlugins/SuperBarPlugin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -181,40 +181,6 @@ type TaskbarBadgedIcon(icon:Icon, ?stacked:Icon list) =
badged.Dispose()
WinUserApi.DestroyIcon(handle) |> ignore

/// A picture the user chose for a group's taskbar button, read as an icon.
module GroupIconFile =
let private pictures = set [".png";".jpg";".jpeg";".bmp";".gif"]
/// An icon that owns its own copy of the bitmap's handle.
let private fromBitmap (bitmap:Bitmap) =
let handle = bitmap.GetHicon()
try (Icon.FromHandle(handle).Clone() :?> Icon) finally WinUserApi.DestroyIcon(handle) |> ignore
/// An .ico, a picture (square and whole, centred), or a program's or library's icon; None when
/// the file is gone or unreadable.
let load (path:string) : Icon option =
try
if not (IO.File.Exists path) then None
else
match IO.Path.GetExtension(path).ToLowerInvariant() with
| ".ico" -> Some(new Icon(path,Size(64,64)))
| extension when pictures.Contains extension ->
use source = new Bitmap(path)
let side = 128
use square = new Bitmap(side,side,PixelFormat.Format32bppArgb)
do
use g = Graphics.FromImage(square)
g.InterpolationMode <- Drawing2D.InterpolationMode.HighQualityBicubic
let scale = float side/float (max source.Width source.Height)
let width,height = max 1 (int(float source.Width*scale)),max 1 (int(float source.Height*scale))
g.DrawImage(source,Rectangle((side-width)/2,(side-height)/2,width,height))
Some(fromBitmap square)
| _ ->
match AppIcons.GetFileIcon(path,64) with
| null -> Some(Icon.ExtractAssociatedIcon(path))
| bitmap ->
use shell = bitmap
Some(fromBitmap shell)
with _ -> None

type TbButton = {
icon : Icon
/// The icons of the group's other apps, front-most first.
Expand Down
13 changes: 2 additions & 11 deletions WtProgram/ManagerViewService/Views/AppGroupingRulesView.fs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,10 @@ module ImgHelper =
module ApplicationNames =
/// ApplicationFrameHost.exe hosts every Store (UWP) app, so it stands for them all, under a
/// name and icon that say so rather than the host's file name and blank icon.
let isWindowsApps (path:string) = AppIdentity.key path = "APPLICATIONFRAMEHOST.EXE"
let isWindowsApps path = WindowsAppsIcon.matches path
let label (path:string) = if isWindowsApps path then tr Strings.AppRules.windowsApps else Path.GetFileName path
/// Four squares in the Windows blue, which reads on light and dark alike.
let windowsAppsIcon() : Image =
let side = Dpi.scale 16
let image = new Bitmap(side,side,Imaging.PixelFormat.Format32bppArgb)
use g = Graphics.FromImage(image)
use brush = new SolidBrush(Color.FromArgb(0,120,212))
let gap = max 1 (side/16)
let square = (side-gap)/2
for x,y in [0,0;square+gap,0;0,square+gap;square+gap,square+gap] do
g.FillRectangle(brush,x,y,square,square)
image :> Image
let windowsAppsIcon() : Image = WindowsAppsIcon.image (Dpi.scale 16) :> Image
/// The icon the app shows on the taskbar (one of its windows), else the executable's own:
/// packaged apps such as Electron ones carry a different icon in the exe than on their
/// windows. A host without icons of its own (ApplicationFrameHost.exe) gets None rather
Expand Down
23 changes: 20 additions & 3 deletions WtProgram/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,23 @@ type Program(lifetime:LifetimeScope, ?settings:Settings) as this =
let isDroppedAndAwaitingGrouping = Cell.create(Set2())
let browserDrags = HashSet<IntPtr>()
let windowColors = WindowTabColors()
let mutable windowIcons : Map<IntPtr,string> = Map.empty
// An immutable map swapped whole on the main thread, so tab strips on group threads
// read renames without waiting for it: tab text is rebuilt on every title change.
let mutable windowNameOverride : Map2<IntPtr,string option> = Map2()
let restoredCustomizations,pendingCustomizationCleanup =
WindowCustomizations.restore WindowCustomizations.identity settingsManager.settingsJson
do
for hwnd,name,color in restoredCustomizations do
for hwnd,name,color,icon in restoredCustomizations do
icon |> Option.iter(fun path -> windowIcons <- windowIcons.Add(hwnd,path))
windowNameOverride <- windowNameOverride.add hwnd name
windowColors.setOverride hwnd color
let saveWindowCustomizations() =
let entries = os.windowsInZorder.toArray |> Array.choose(fun window ->
let name = windowNameOverride.tryFind(window.hwnd).bind(id)
let color = windowColors.getOverride window.hwnd
if name.IsNone && color.IsNone then None else Some(window.hwnd,name,color)) |> Array.toList
let icon = windowIcons.TryFind window.hwnd
if name.IsNone && color.IsNone && icon.IsNone then None else Some(window.hwnd,name,color,icon)) |> Array.toList
let root = settingsManager.settingsJson
root.["windowCustomizations"] <- WindowCustomizations.write WindowCustomizations.identity entries
settingsManager.settingsJson <- root
Expand Down Expand Up @@ -1095,6 +1098,7 @@ type Program(lifetime:LifetimeScope, ?settings:Settings) as this =
isDroppedAndAwaitingGrouping.map(fun s -> s.remove hwnd)
windowNameOverride <- windowNameOverride.remove hwnd
windowColors.remove hwnd
windowIcons <- windowIcons.Remove hwnd
VirtualDesktop.forget hwnd
awaitingDesktop.Remove hwnd |> ignore

Expand Down Expand Up @@ -1817,6 +1821,15 @@ type Program(lifetime:LifetimeScope, ?settings:Settings) as this =
member x.getWindowNameOverride(hwnd) =
windowNameOverride.tryFind(hwnd).bind(id)

member _.getTabIcon hwnd = windowIcons.TryFind hwnd
member _.setTabIcon(hwnd,path) =
windowIcons <- match path with Some path -> windowIcons.Add(hwnd,path) | None -> windowIcons.Remove hwnd
saveWindowCustomizations()
path |> Option.iter(fun path ->
let root = settingsManager.settingsJson
TabIconChoices.remember path root
settingsManager.settingsJson <- root)

member _.getTabColorOverride hwnd = windowColors.getOverride hwnd
member _.setTabColorOverride((hwnd,color)) =
windowColors.setOverride hwnd color
Expand Down Expand Up @@ -1903,7 +1916,11 @@ type Program(lifetime:LifetimeScope, ?settings:Settings) as this =
member x.setGroupIcon(hwnd,path) =
this.groupIconKey hwnd |> Option.iter(fun key ->
let icons = settingsManager.groupIcons
settingsManager.groupIcons <- match path with Some path -> icons.Add(key,path) | None -> icons.Remove key)
settingsManager.groupIcons <- match path with Some path -> icons.Add(key,path) | None -> icons.Remove key
path |> Option.iter(fun path ->
let root = settingsManager.settingsJson
TabIconChoices.remember path root
settingsManager.settingsJson <- root))

member x.appWindows =
os.windowsInZorder.where(this.isAppWindow).map(fun w -> w.hwnd)
Expand Down
3 changes: 3 additions & 0 deletions WtProgram/Shared/ProgramTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ type IProgram =
abstract member shutdown : unit -> unit
abstract member setWindowNameOverride : (IntPtr * Option<string>) -> unit
abstract member getWindowNameOverride : IntPtr -> Option<string>
abstract member getTabIcon : IntPtr -> string option
abstract member setTabIcon : IntPtr * string option -> unit
abstract member getTabColorOverride : IntPtr -> TabColorChoice option
abstract member setTabColorOverride : (IntPtr * TabColorChoice option) -> unit
abstract member getTabColor : IntPtr * IntPtr list -> TabColorChoice option
Expand Down Expand Up @@ -288,6 +290,7 @@ type GroupCommand =
/// The strip's order: the windows named, in that order, then any not named in the order
/// they had; the pinned ones kept first either way.
| SetTabOrder of IntPtr list
| SetTabIcon of IntPtr * string option
| SetTabColor of IntPtr * TabColorChoice option
| SetTabPinned of IntPtr * bool
| ToggleIconsOnly
Expand Down
2 changes: 2 additions & 0 deletions WtProgram/Shared/Services.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type DispatchedProgram(inner:IProgram, dispatcher:IDispatcher) =
member _.setWindowNameOverride value = dispatcher.Send(fun () -> inner.setWindowNameOverride value)
// Reads an immutable snapshot; called by tab strips on every title change.
member _.getWindowNameOverride hwnd = inner.getWindowNameOverride hwnd
member _.getTabIcon hwnd = inner.getTabIcon hwnd
member _.setTabIcon(hwnd,path) = dispatcher.Send(fun () -> inner.setTabIcon(hwnd,path))
member _.getTabColorOverride hwnd = inner.getTabColorOverride hwnd
member _.setTabColorOverride value = dispatcher.Send(fun () -> inner.setTabColorOverride value)
member _.getTabColor(hwnd,peers) = dispatcher.Send(fun () -> inner.getTabColor(hwnd,peers))
Expand Down
Loading