Skip to content

Commit

Permalink
- Window:
Browse files Browse the repository at this point in the history
  - Move `setIcon` to `WindowBase`.
  • Loading branch information
gmpassos committed Aug 10, 2023
1 parent 8741f49 commit d6548ea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

- `WindowBase`:
- Generalize `create`.
- `Window`:
- Move `setIcon` to `WindowBase`.
- `Dialog.create`: call `ensureLoaded`.

## 1.0.12
Expand Down
82 changes: 41 additions & 41 deletions lib/src/win32_gui_base.dart
Expand Up @@ -722,6 +722,47 @@ abstract class WindowBase<W extends WindowBase<W>> {
}
}

int? _iconSmall;
int? _iconBig;

/// Sets this [Window] icon from [iconPath].
///
/// - If [small] is true, sets a 16x16 icon.
/// - If [big] is true, sets a 48x48 or 32x32 icon.
/// - If [cached] is true will load the icons using [loadIconCached], otherwise will call [loadIcon].
/// - If [force] is true will always call [sendMessage], even if the icon was already set to the same icon handler.
void setIcon(String iconPath,
{bool small = true,
bool big = true,
bool cached = true,
bool force = false}) {
var loader = cached ? Window.loadIconCached : Window.loadIcon;

if (small) {
var hIcon = loader(iconPath, 16, 16);
if (hIcon == 0) {
hIcon = loader(iconPath, 32, 32);
}

if (force || _iconSmall != hIcon) {
sendMessage(WM_SETICON, ICON_SMALL2, hIcon);
_iconSmall = hIcon;
}
}

if (big) {
var hIcon = loader(iconPath, 48, 48);
if (hIcon == 0) {
hIcon = loader(iconPath, 32, 32);
}

if (force || _iconBig != hIcon) {
sendMessage(WM_SETICON, ICON_BIG, hIcon);
_iconBig = hIcon;
}
}
}

/// Shows this [Window].
/// - Calls Win32 [ShowWindow] [SW_SHOWNORMAL].
void show() {
Expand Down Expand Up @@ -1274,47 +1315,6 @@ class Window extends WindowBase<Window> {
return dimension;
}

int? _iconSmall;
int? _iconBig;

/// Sets this [Window] icon from [iconPath].
///
/// - If [small] is true, sets a 16x16 icon.
/// - If [big] is true, sets a 48x48 or 32x32 icon.
/// - If [cached] is true will load the icons using [loadIconCached], otherwise will call [loadIcon].
/// - If [force] is true will always call [sendMessage], even if the icon was already set to the same icon handler.
void setIcon(String iconPath,
{bool small = true,
bool big = true,
bool cached = true,
bool force = false}) {
var loader = cached ? loadIconCached : loadIcon;

if (small) {
var hIcon = loader(iconPath, 16, 16);
if (hIcon == 0) {
hIcon = loader(iconPath, 32, 32);
}

if (force || _iconSmall != hIcon) {
sendMessage(WM_SETICON, ICON_SMALL2, hIcon);
_iconSmall = hIcon;
}
}

if (big) {
var hIcon = loader(iconPath, 48, 48);
if (hIcon == 0) {
hIcon = loader(iconPath, 32, 32);
}

if (force || _iconBig != hIcon) {
sendMessage(WM_SETICON, ICON_BIG, hIcon);
_iconBig = hIcon;
}
}
}

static final Map<String, int> _iconsCache = {};

static int loadIconCached(String iconPath, int width, int height) {
Expand Down

0 comments on commit d6548ea

Please sign in to comment.