Skip to content

Commit 01e6e6a

Browse files
committed
SwtMisc.systemFontWidth now returns double instead of int`.
1 parent 53338fd commit 01e6e6a

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

CHANGES.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## [Unreleased]
44
### Changed
5-
- **BREAKING** remove RxJava completely in favor of Kotlin Flow
5+
- **BREAKING** `SwtMisc.systemFontWidth` now returns `double` instead of `int`.
6+
- added new methods `systemFontWidthTimes(int)` and `systemFontWidthTimes(String)` to make this easier to deal with
7+
- **BREAKING** remove RxJava completely in favor of Kotlin Flow.
68
- Bump required java from 11 to 17.
79

810
## [4.3.1] - 2024-07-05

durian-swt/src/main/java/com/diffplug/common/swt/SwtMisc.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.common.swt;
1717

18-
1918
import com.diffplug.common.base.Box;
2019
import com.diffplug.common.base.Errors;
2120
import com.diffplug.common.base.Preconditions;
@@ -358,7 +357,7 @@ public static int blockForMessageBox(String title, String message, int style) {
358357
///////////////////
359358
/** The cached height of the system font. */
360359
static int systemFontHeight = 0;
361-
static int systemFontWidth = 0;
360+
static double systemFontWidth = 0;
362361

363362
/** Populates the height and width of the system font. */
364363
private static void populateSystemFont() {
@@ -368,7 +367,7 @@ private static void populateSystemFont() {
368367

369368
FontMetrics metrics = gc.getFontMetrics();
370369
systemFontHeight = metrics.getHeight();
371-
systemFontWidth = metrics.getAverageCharWidth();
370+
systemFontWidth = metrics.getAverageCharacterWidth();
372371
if (OS.getNative().isMac()) {
373372
// add 20% width on Mac
374373
systemFontWidth = (systemFontWidth * 12) / 10;
@@ -387,26 +386,34 @@ public static int systemFontHeight() {
387386
}
388387

389388
/** Returns the width of the system font. */
390-
public static int systemFontWidth() {
389+
public static double systemFontWidth() {
391390
if (systemFontWidth == 0) {
392391
populateSystemFont();
393392
}
394393
return systemFontWidth;
395394
}
396395

396+
public static int systemFontWidthTimes(int numChars) {
397+
return (int) Math.round(systemFontWidth() * numChars);
398+
}
399+
400+
public static int systemFontWidthTimes(String str) {
401+
return systemFontWidthTimes(str.length());
402+
}
403+
397404
/** Returns a distance which is a snug fit for a line of text in the system font. */
398405
public static int systemFontSnug() {
399406
return systemFontHeight() + Layouts.defaultMargin();
400407
}
401408

402409
/** Returns the default width of a button, scaled for the system font. */
403410
public static int defaultButtonWidth() {
404-
return systemFontWidth() * " Cancel ".length();
411+
return systemFontWidthTimes(" Cancel ");
405412
}
406413

407414
/** Returns the default width of a dialog. */
408415
public static int defaultDialogWidth() {
409-
return 50 * systemFontWidth();
416+
return systemFontWidthTimes(50);
410417
}
411418

412419
/** Returns a size which is scaled by the system font's height. */
@@ -421,7 +428,7 @@ public static int scaleByFontHeight(int rows) {
421428

422429
/** Returns a point that represents the size of a (cols x rows) grid of characters printed in the standard system font. */
423430
public static Point scaleByFont(int cols, int rows) {
424-
return new Point(cols * systemFontWidth(), rows * systemFontHeight());
431+
return new Point(systemFontWidthTimes(cols), rows * systemFontHeight());
425432
}
426433

427434
/** Returns a dimension which is guaranteed to be comfortable for the given string. */

durian-swt/src/main/java/com/diffplug/common/swt/widgets/ScaleCtl.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.common.swt.widgets;
1717

18-
1918
import com.diffplug.common.base.Preconditions;
2019
import com.diffplug.common.rx.RxBox;
2120
import com.diffplug.common.swt.ControlWrapper;
@@ -136,7 +135,7 @@ public ScaleCtl(Composite parent, Builder builder, RxBox<Double> box) {
136135
}
137136

138137
text = new Text(wrapped, SWT.BORDER | SWT.SINGLE);
139-
Layouts.setGridData(text).grabHorizontal().widthHint(SwtMisc.systemFontWidth() * 4);
138+
Layouts.setGridData(text).grabHorizontal().widthHint(SwtMisc.systemFontWidthTimes(4));
140139

141140
SwtExec.async().guardOn(text).execute(text::selectAll);
142141
Listener selectAllListener = SwtMisc.asListener(SwtExec.async().guardOn(text).wrap(text::selectAll));

0 commit comments

Comments
 (0)