libui/windows: cache ideal size for uiButton.

input dialog isn't abysmally slow anymore.
This commit is contained in:
StapleButter 2018-12-14 01:15:18 +01:00
parent dc2b1219cd
commit e829c2e4a1
1 changed files with 19 additions and 0 deletions

View File

@ -6,6 +6,9 @@ struct uiButton {
HWND hwnd; HWND hwnd;
void (*onClicked)(uiButton *, void *); void (*onClicked)(uiButton *, void *);
void *onClickedData; void *onClickedData;
SIZE idealSize;
int idealSizeCached;
}; };
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult) static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
@ -41,6 +44,13 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height)
uiWindowsSizing sizing; uiWindowsSizing sizing;
int y; int y;
if (b->idealSizeCached)
{
*width = b->idealSize.cx;
*height = b->idealSize.cy;
return;
}
// try the comctl32 version 6 way // try the comctl32 version 6 way
size.cx = 0; // explicitly ask for ideal size size.cx = 0; // explicitly ask for ideal size
size.cy = 0; size.cy = 0;
@ -48,6 +58,9 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height)
*width = size.cx; *width = size.cx;
if (*width < buttonMinWidth) *width = buttonMinWidth; if (*width < buttonMinWidth) *width = buttonMinWidth;
*height = size.cy; *height = size.cy;
b->idealSize.cx = *width;
b->idealSize.cy = *height;
b->idealSizeCached = true;
return; return;
} }
@ -60,6 +73,9 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height)
uiWindowsGetSizing(b->hwnd, &sizing); uiWindowsGetSizing(b->hwnd, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &y); uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &y);
*height = y; *height = y;
b->idealSize.cx = *width;
b->idealSize.cy = *height;
b->idealSizeCached = true;
} }
static void defaultOnClicked(uiButton *b, void *data) static void defaultOnClicked(uiButton *b, void *data)
@ -75,6 +91,7 @@ char *uiButtonText(uiButton *b)
void uiButtonSetText(uiButton *b, const char *text) void uiButtonSetText(uiButton *b, const char *text)
{ {
uiWindowsSetWindowText(b->hwnd, text); uiWindowsSetWindowText(b->hwnd, text);
b->idealSizeCached = 0;
// changing the text might necessitate a change in the button's size // changing the text might necessitate a change in the button's size
uiWindowsControlMinimumSizeChanged(uiWindowsControl(b)); uiWindowsControlMinimumSizeChanged(uiWindowsControl(b));
} }
@ -103,5 +120,7 @@ uiButton *uiNewButton(const char *text)
uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b)); uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b));
uiButtonOnClicked(b, defaultOnClicked, NULL); uiButtonOnClicked(b, defaultOnClicked, NULL);
b->idealSizeCached = 0;
return b; return b;
} }