4
4
// cSpell: ignore qstyle unshade
5
5
6
6
use super :: * ;
7
+ use i_slint_core:: graphics:: euclid;
7
8
8
9
#[ allow( nonstandard_style) ]
9
10
#[ allow( unused) ]
@@ -107,6 +108,7 @@ type ActualStandardButtonKind = Option<StandardButtonKind>;
107
108
pub struct NativeButton {
108
109
pub text : Property < SharedString > ,
109
110
pub icon : Property < i_slint_core:: graphics:: Image > ,
111
+ pub icon_size : Property < LogicalLength > ,
110
112
pub pressed : Property < bool > ,
111
113
pub has_hover : Property < bool > ,
112
114
pub checkable : Property < bool > ,
@@ -166,7 +168,9 @@ impl NativeButton {
166
168
Some ( StandardButtonKind :: Retry ) => QStyle_StandardPixmap_SP_DialogRetryButton ,
167
169
Some ( StandardButtonKind :: Ignore ) => QStyle_StandardPixmap_SP_DialogIgnoreButton ,
168
170
None => {
169
- return crate :: qt_window:: image_to_pixmap ( ( & self . icon ( ) ) . into ( ) , None )
171
+ let icon_size = self . icon_size ( ) . get ( ) . round ( ) as u32 ;
172
+ let source_size = Some ( euclid:: Size2D :: new ( icon_size, icon_size) ) ;
173
+ return crate :: qt_window:: image_to_pixmap ( ( & self . icon ( ) ) . into ( ) , source_size)
170
174
. unwrap_or_default ( ) ;
171
175
}
172
176
} ;
@@ -195,7 +199,19 @@ impl Item for NativeButton {
195
199
let animation_tracker_property_ptr = Self :: FIELD_OFFSETS . animation_tracker . apply_pin ( self ) ;
196
200
self . widget_ptr . set ( cpp ! { unsafe [ animation_tracker_property_ptr as "void*" ] -> SlintTypeErasedWidgetPtr as "std::unique_ptr<SlintTypeErasedWidget>" {
197
201
return make_unique_animated_widget<QPushButton >( animation_tracker_property_ptr) ;
198
- } } )
202
+ } } ) ;
203
+ let widget_ptr: NonNull < ( ) > = SlintTypeErasedWidgetPtr :: qwidget_ptr ( & self . widget_ptr ) ;
204
+ let icon_size = unsafe {
205
+ cpp ! ( [ widget_ptr as "QWidget*" ] -> i32 as "int"
206
+ {
207
+ ensure_initialized( ) ;
208
+ return qApp->style( ) ->pixelMetric( QStyle :: PM_ButtonIconSize , 0 , widget_ptr) ;
209
+ } )
210
+ } ;
211
+ Self :: FIELD_OFFSETS
212
+ . icon_size
213
+ . apply_pin ( self )
214
+ . set ( LogicalLength :: new ( icon_size as i_slint_core:: Coord ) ) ;
199
215
}
200
216
201
217
fn layout_info (
@@ -207,10 +223,12 @@ impl Item for NativeButton {
207
223
let standard_button_kind = self . actual_standard_button_kind ( ) ;
208
224
let mut text: qttypes:: QString = self . actual_text ( standard_button_kind) ;
209
225
let icon: qttypes:: QPixmap = self . actual_icon ( standard_button_kind) ;
226
+ let icon_size = self . icon_size ( ) . get ( ) as i32 ;
210
227
let widget_ptr: NonNull < ( ) > = SlintTypeErasedWidgetPtr :: qwidget_ptr ( & self . widget_ptr ) ;
211
228
let size = cpp ! ( unsafe [
212
229
mut text as "QString" ,
213
230
icon as "QPixmap" ,
231
+ icon_size as "int" ,
214
232
widget_ptr as "QWidget*"
215
233
] -> qttypes:: QSize as "QSize" {
216
234
ensure_initialized( ) ;
@@ -220,11 +238,10 @@ impl Item for NativeButton {
220
238
option. rect = option. fontMetrics. boundingRect( text) ;
221
239
option. text = std:: move( text) ;
222
240
option. icon = icon;
223
- auto iconSize = qApp->style( ) ->pixelMetric( QStyle :: PM_ButtonIconSize , 0 , widget_ptr) ;
224
- option. iconSize = QSize ( iconSize, iconSize) ;
241
+ option. iconSize = QSize ( icon_size, icon_size) ;
225
242
if ( !icon. isNull( ) ) {
226
- option. rect. setHeight( qMax( option. rect. height( ) , iconSize ) ) ;
227
- option. rect. setWidth( option. rect. width( ) + 4 + iconSize ) ;
243
+ option. rect. setHeight( qMax( option. rect. height( ) , icon_size ) ) ;
244
+ option. rect. setWidth( option. rect. width( ) + 4 + icon_size ) ;
228
245
}
229
246
return qApp->style( ) ->sizeFromContents( QStyle :: CT_PushButton , & option, option. rect. size( ) , widget_ptr) ;
230
247
} ) ;
@@ -348,6 +365,7 @@ impl Item for NativeButton {
348
365
let has_focus = this. has_focus( ) ;
349
366
let has_hover = this. has_hover( ) ;
350
367
let primary = this. primary( ) ;
368
+ let icon_size = this. icon_size( ) . get( ) . round( ) as i32 ;
351
369
let colorize_icon = this. colorize_icon( ) ;
352
370
353
371
cpp!( unsafe [
@@ -362,6 +380,7 @@ impl Item for NativeButton {
362
380
has_focus as "bool" ,
363
381
has_hover as "bool" ,
364
382
primary as "bool" ,
383
+ icon_size as "int" ,
365
384
colorize_icon as "bool" ,
366
385
dpr as "float" ,
367
386
initial_state as "int"
@@ -429,8 +448,7 @@ impl Item for NativeButton {
429
448
} else {
430
449
option. icon = icon;
431
450
}
432
- auto iconSize = qApp->style( ) ->pixelMetric( QStyle :: PM_ButtonIconSize , 0 , nullptr) ;
433
- option. iconSize = QSize ( iconSize, iconSize) ;
451
+ option. iconSize = QSize ( icon_size, icon_size) ;
434
452
option. rect = QRect ( QPoint ( ) , size / dpr) ;
435
453
436
454
qApp->style( ) ->drawControl( QStyle :: CE_PushButton , & option, painter->get( ) , widget) ;
0 commit comments