forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbar.cpp
More file actions
145 lines (124 loc) · 4.39 KB
/
Toolbar.cpp
File metadata and controls
145 lines (124 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "Toolbar.h"
#include "AppWindow.h"
#include "resource.h"
Toolbar::Toolbar() : m_items{ Item_LAST } {}
Toolbar::~Toolbar()
{
if (m_font)
{
DeleteObject(m_font);
}
}
void Toolbar::Initialize(AppWindow* appWindow)
{
m_appWindow = appWindow;
HWND mainWindow = m_appWindow->GetMainWindow();
m_items[Item_BackButton] = CreateWindow(
L"button", L"Back", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 0, 0, 0, 0,
mainWindow, (HMENU)IDE_BACK, nullptr, 0);
m_items[Item_ForwardButton] = CreateWindow(
L"button", L"Forward", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 0, 0, 0, 0,
mainWindow, (HMENU)IDE_FORWARD, nullptr, 0);
m_items[Item_ReloadButton] = CreateWindow(
L"button", L"Reload", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 0, 0, 0, 0,
mainWindow, (HMENU)IDE_ADDRESSBAR_RELOAD, nullptr, 0);
m_items[Item_CancelButton] = CreateWindow(
L"button", L"Cancel", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 0, 0, 0, 0,
mainWindow, (HMENU)IDE_CANCEL, nullptr, 0);
m_items[Item_AddressBar] = CreateWindow(
L"edit", nullptr, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 0, 0, 0, 0,
mainWindow, (HMENU)IDE_ADDRESSBAR, nullptr, 0);
m_items[Item_GoButton] = CreateWindow(
L"button", L"Go", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | BS_DEFPUSHBUTTON,
0, 0, 0, 0, mainWindow, (HMENU)IDE_ADDRESSBAR_GO, nullptr, 0);
UpdateDpiAndTextScale();
RECT availableBounds = { 0 };
GetClientRect(mainWindow, &availableBounds);
Resize(availableBounds);
DisableAllItems();
}
void Toolbar::SetItemEnabled(Item item, bool enabled)
{
EnableWindow(m_items[item], enabled);
}
void Toolbar::DisableAllItems()
{
for (HWND hwnd : m_items)
{
EnableWindow(hwnd, FALSE);
}
}
HWND Toolbar::GetItem(Item item) const
{
return m_items[item];
}
const std::vector<HWND>& Toolbar::GetItems() const
{
return m_items;
}
RECT Toolbar::Resize(RECT availableBounds)
{
const int clientWidth = availableBounds.right - availableBounds.left;
const int clientHeight = availableBounds.bottom - availableBounds.top;
const float dpiScale = m_appWindow->GetDpiScale();
const int clientLogicalWidth = clientWidth / dpiScale;
const int itemHeight = 32 * dpiScale;
int nextOffsetX = 0;
for (Item item = Item_BackButton; item < Item_LAST; item = Item(item + 1))
{
int itemWidth = GetItemLogicalWidth(item, clientLogicalWidth) * dpiScale;
SetWindowPos(m_items[item], nullptr, nextOffsetX, 0, itemWidth, itemHeight,
SWP_NOZORDER | SWP_NOACTIVATE);
nextOffsetX += itemWidth;
}
return { 0, itemHeight, clientWidth, clientHeight };
}
void Toolbar::UpdateDpiAndTextScale()
{
UpdateFont();
for (Item item = Item_BackButton; item < Item_LAST; item = Item(item + 1))
{
SendMessage(m_items[item], WM_SETFONT, (WPARAM)m_font, FALSE);
}
}
int Toolbar::GetItemLogicalWidth(Item item, int clientLogicalWidth) const
{
static const int s_itemButtonLogicalWidth = 64;
int itemLogicalWidth = 0;
switch (item)
{
case Item_BackButton:
case Item_ForwardButton:
case Item_ReloadButton:
case Item_CancelButton:
case Item_GoButton:
itemLogicalWidth = s_itemButtonLogicalWidth;
break;
case Item_AddressBar:
itemLogicalWidth = clientLogicalWidth - s_itemButtonLogicalWidth * (Item_LAST - 1);
break;
default:
FAIL_FAST();
}
return itemLogicalWidth;
}
void Toolbar::UpdateFont()
{
static const WCHAR s_fontName[] = L"WebView2APISample Font";
if (m_font)
{
DeleteObject(m_font);
}
LOGFONT logFont;
GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &logFont);
double dpiScale = m_appWindow->GetDpiScale();
double textScale = m_appWindow->GetTextScale();
logFont.lfHeight *= dpiScale * textScale;
logFont.lfWidth *= dpiScale * textScale;
StringCchCopy(logFont.lfFaceName, ARRAYSIZE(logFont.lfFaceName), s_fontName);
m_font = CreateFontIndirect(&logFont);
}