Skip to content

Commit cdcb692

Browse files
authored
Merge pull request #60 from Chaoses-Ib/feat/win32-tooltip-multiline
feat(win32): multiline tooltip
2 parents c5f4b20 + 6521a3e commit cdcb692

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

winio-ui-win32/src/ui/tooltip.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use widestring::U16CString;
77
use windows_sys::Win32::UI::{
88
Controls::{
99
TOOLTIPS_CLASSW, TTF_IDISHWND, TTF_SUBCLASS, TTM_ADDTOOLW, TTM_DELTOOLW,
10-
TTM_UPDATETIPTEXTW, TTS_ALWAYSTIP, TTS_NOPREFIX, TTTOOLINFOW,
10+
TTM_SETMAXTIPWIDTH, TTM_UPDATETIPTEXTW, TTS_ALWAYSTIP, TTS_NOPREFIX, TTTOOLINFOW,
1111
},
12-
WindowsAndMessaging::{DestroyWindow, GetParent, WS_POPUP},
12+
WindowsAndMessaging::{DestroyWindow, GetParent, GetSystemMetrics, SM_CXMAXTRACK, WS_POPUP},
1313
};
1414
use winio_handle::{AsRawWidget, AsWidget};
1515

16-
use crate::Widget;
16+
use crate::{Widget, ui::fix_crlf};
1717

1818
pub struct ToolTip<T: AsWidget> {
1919
inner: T,
@@ -31,6 +31,12 @@ impl<T: AsWidget> ToolTip<T> {
3131
0,
3232
parent,
3333
);
34+
35+
// Enable support for multiline tooltips
36+
// -1 doesn't work, we use SM_CXMAXTRACK like WinForms does
37+
let max_width = unsafe { GetSystemMetrics(SM_CXMAXTRACK) };
38+
handle.send_message(TTM_SETMAXTIPWIDTH, 0, max_width as isize);
39+
3440
let mut info: TTTOOLINFOW = unsafe { std::mem::zeroed() };
3541
info.cbSize = std::mem::size_of::<TTTOOLINFOW>() as _;
3642
info.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
@@ -44,7 +50,7 @@ impl<T: AsWidget> ToolTip<T> {
4450
}
4551

4652
pub fn tooltip(&self) -> String {
47-
self.text.to_string_lossy()
53+
self.text.to_string_lossy().replace("\r\n", "\n")
4854
}
4955

5056
fn update_info(&mut self, msg: u32) {
@@ -57,7 +63,7 @@ impl<T: AsWidget> ToolTip<T> {
5763

5864
pub fn set_tooltip(&mut self, s: impl AsRef<str>) {
5965
let add_new = self.text.is_empty();
60-
self.text = U16CString::from_str_truncate(s);
66+
self.text = U16CString::from_str_truncate(fix_crlf(s.as_ref()));
6167
if self.text.is_empty() {
6268
self.delete();
6369
} else {

winio/examples/widgets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Component for MainModel {
106106
},
107107
show_button: ToolTip<Button> = (&window) => {
108108
text: "Show",
109-
tooltip: "Show the current selection in the combo box."
109+
tooltip: "Show the current selection in the combo box.\nIf no selection, show \"No selection.\"",
110110
},
111111
progress: Progress = (&window) => {
112112
indeterminate: true,

0 commit comments

Comments
 (0)