Skip to content

Commit

Permalink
Remove cruft from ThemeMac
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=266539

Reviewed by Aditya Keerthi.

At this point in its life ThemeMac is mainly responsible for
determining the sizing of a couple of form controls as the GPU process
has taken over most of the theming aspects.

And to determine the sizing of these form controls no cells are needed
so all that code can be removed.

And then we also remove some code that eventually does not do anything,
such as StyleAppearance::ColorWell which do to not having the correct
bezel style (which is itself a concept that can be removed) never ends
up being impacted by inflateControlPaintRect().

* Source/WebCore/platform/Theme.h:
(WebCore::Theme::inflateControlPaintRect const):
* Source/WebCore/platform/mac/ThemeMac.h:
* Source/WebCore/platform/mac/ThemeMac.mm:
(WebCore::radioSize):
(WebCore::ThemeMac::inflateControlPaintRect const):
(WebCore::setControlSize): Deleted.
(WebCore::updateStates): Deleted.
(WebCore::configureToggleButton): Deleted.
(WebCore::createToggleButtonCell): Deleted.
(WebCore::sharedRadioCell): Deleted.
(WebCore::sharedCheckboxCell): Deleted.
(WebCore::buttonCell): Deleted.
(WebCore::setUpButtonCell): Deleted.
(WebCore::button): Deleted.
* Source/WebCore/rendering/RenderThemeMac.mm:
(WebCore::RenderThemeMac::adjustRepaintRect):

Canonical link: https://commits.webkit.org/272218@main
  • Loading branch information
annevk committed Dec 18, 2023
1 parent 58de43f commit 20d6a49
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 170 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/platform/Theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Theme {
// the theme needs to communicate this inflated rect to the engine so that it can invalidate the whole control.
// The rect passed in is in zoomed coordinates, so the inflation should take that into account and make sure the inflation
// amount is also scaled by the zoomFactor.
virtual void inflateControlPaintRect(StyleAppearance, OptionSet<ControlStyle::State>, FloatRect&, float) const { }
virtual void inflateControlPaintRect(StyleAppearance, FloatRect&, float) const { }

virtual void drawNamedImage(const String&, GraphicsContext&, const FloatSize&) const;

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/mac/ThemeMac.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ThemeMac final : public ThemeCocoa {

bool controlRequiresPreWhiteSpace(StyleAppearance appearance) const final { return appearance == StyleAppearance::PushButton; }

void inflateControlPaintRect(StyleAppearance, OptionSet<ControlStyle::State>, FloatRect&, float zoomFactor) const final;
void inflateControlPaintRect(StyleAppearance, FloatRect&, float zoomFactor) const final;

bool userPrefersContrast() const final;
bool userPrefersReducedMotion() const final;
Expand Down
176 changes: 16 additions & 160 deletions Source/WebCore/platform/mac/ThemeMac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@

#if PLATFORM(MAC)

#import "AXObjectCache.h"
#import "ColorMac.h"
#import "GraphicsContext.h"
#import "GraphicsContextCG.h"
#import "ImageBuffer.h"
#import "FontCascade.h"
#import "LengthSize.h"
#import "LocalDefaultSystemAppearance.h"
#import "ScrollView.h"
#import <pal/spi/cocoa/NSButtonCellSPI.h>
#import <pal/spi/mac/CoreUISPI.h>
#import <pal/spi/mac/NSAppearanceSPI.h>
#import <pal/spi/mac/NSGraphicsSPI.h>
Expand Down Expand Up @@ -194,48 +188,6 @@ static NSControlSize controlSizeFromPixelSize(const std::array<IntSize, 4>& size
return NSControlSizeMini;
}

static void setControlSize(NSCell* cell, const std::array<IntSize, 4>& sizes, const IntSize& minZoomedSize, float zoomFactor)
{
auto size = controlSizeFromPixelSize(sizes, minZoomedSize, zoomFactor);
if (size != [cell controlSize]) // Only update if we have to, since AppKit does work even if the size is the same.
[cell setControlSize:size];
}

static void updateStates(NSCell* cell, OptionSet<ControlStyle::State> states)
{
// Hover state is not supported by Aqua.

// Pressed state
bool oldPressed = [cell isHighlighted];
bool pressed = states.contains(ControlStyle::State::Pressed);
if (pressed != oldPressed) {
[(NSButtonCell*)cell _setHighlighted:pressed animated:NO];
}

// Enabled state
bool oldEnabled = [cell isEnabled];
bool enabled = states.contains(ControlStyle::State::Enabled);
if (enabled != oldEnabled)
[cell setEnabled:enabled];

// Checked and Indeterminate
bool oldIndeterminate = [cell state] == NSControlStateValueMixed;
bool indeterminate = states.contains(ControlStyle::State::Indeterminate);
bool checked = states.contains(ControlStyle::State::Checked);
bool oldChecked = [cell state] == NSControlStateValueOn;
if (oldIndeterminate != indeterminate || checked != oldChecked) {
NSControlStateValue newState = indeterminate ? NSControlStateValueMixed : (checked ? NSControlStateValueOn : NSControlStateValueOff);
[(NSButtonCell*)cell _setState:newState animated:NO];
}

// Presenting state
if (states.contains(ControlStyle::State::Presenting))
[(NSButtonCell*)cell _setHighlighted:YES animated:NO];

// Window inactive state does not need to be checked explicitly, since we paint parented to
// a view in a window whose key state can be detected.
}

static FloatRect inflateRect(const FloatRect& zoomedRect, const IntSize& zoomedSize, const int* margins, float zoomFactor)
{
// Only do the inflation if the available width/height are too small.
Expand Down Expand Up @@ -321,45 +273,6 @@ static LengthSize radioSize(const LengthSize& zoomedSize, float zoomFactor)

return sizeFromNSControlSize(NSControlSizeSmall, zoomedSize, zoomFactor, radioSizes());
}

static void configureToggleButton(NSCell* cell, StyleAppearance appearance, OptionSet<ControlStyle::State> states, const IntSize& zoomedSize, float zoomFactor)
{
setControlSize(cell, appearance == StyleAppearance::Checkbox ? checkboxSizes() : radioSizes(), zoomedSize, zoomFactor);
updateStates(cell, states);
}

static RetainPtr<NSButtonCell> createToggleButtonCell(StyleAppearance appearance)
{
RetainPtr<NSButtonCell> toggleButtonCell = adoptNS([[NSButtonCell alloc] init]);

if (appearance == StyleAppearance::Checkbox) {
[toggleButtonCell setButtonType:NSButtonTypeSwitch];
[toggleButtonCell setAllowsMixedState:YES];
} else {
ASSERT(appearance == StyleAppearance::Radio);
[toggleButtonCell setButtonType:NSButtonTypeRadio];
}

[toggleButtonCell setTitle:nil];
[toggleButtonCell setFocusRingType:NSFocusRingTypeExterior];
return toggleButtonCell;
}

static NSButtonCell *sharedRadioCell(OptionSet<ControlStyle::State> states, const IntSize& zoomedSize, float zoomFactor)
{
static NSButtonCell *radioCell = createToggleButtonCell(StyleAppearance::Radio).leakRef();

configureToggleButton(radioCell, StyleAppearance::Radio, states, zoomedSize, zoomFactor);
return radioCell;
}

static NSButtonCell *sharedCheckboxCell(OptionSet<ControlStyle::State> states, const IntSize& zoomedSize, float zoomFactor)
{
static NSButtonCell *checkboxCell = createToggleButtonCell(StyleAppearance::Checkbox).leakRef();

configureToggleButton(checkboxCell, StyleAppearance::Checkbox, states, zoomedSize, zoomFactor);
return checkboxCell;
}

// Buttons

Expand All @@ -385,55 +298,6 @@ static void configureToggleButton(NSCell* cell, StyleAppearance appearance, Opti
return margins[controlSize];
}

enum ButtonCellType { NormalButtonCell, DefaultButtonCell };

static RetainPtr<NSButtonCell> buttonCell(ButtonCellType type)
{
auto cell = adoptNS([[NSButtonCell alloc] init]);
[cell setTitle:nil];
[cell setButtonType:NSButtonTypeMomentaryPushIn];
if (type == DefaultButtonCell)
[cell setKeyEquivalent:@"\r"];
return cell;
}

static void setUpButtonCell(NSButtonCell *cell, StyleAppearance appearance, OptionSet<ControlStyle::State> states, const IntSize& zoomedSize, float zoomFactor)
{
const std::array<IntSize, 4>& sizes = buttonSizes();
switch (appearance) {
case StyleAppearance::SquareButton:
[cell setBezelStyle:NSBezelStyleShadowlessSquare];
break;
#if ENABLE(INPUT_TYPE_COLOR)
case StyleAppearance::ColorWell:
[cell setBezelStyle:NSBezelStyleTexturedSquare];
break;
#endif
default:
auto largestControlSize = ThemeMac::supportsLargeFormControls() ? NSControlSizeLarge : NSControlSizeRegular;
NSBezelStyle style = (zoomedSize.height() > buttonSizes()[largestControlSize].height() * zoomFactor) ? NSBezelStyleShadowlessSquare : NSBezelStyleRounded;
[cell setBezelStyle:style];
break;
}

setControlSize(cell, sizes, zoomedSize, zoomFactor);
updateStates(cell, states);
}

static NSButtonCell *button(StyleAppearance appearance, OptionSet<ControlStyle::State> states, const IntSize& zoomedSize, float zoomFactor)
{
NSButtonCell *cell;
if (states.contains(ControlStyle::State::Default)) {
static NeverDestroyed<RetainPtr<NSButtonCell>> defaultCell = buttonCell(DefaultButtonCell);
cell = defaultCell.get().get();
} else {
static NeverDestroyed<RetainPtr<NSButtonCell>> normalCell = buttonCell(NormalButtonCell);
cell = normalCell.get().get();
}
setUpButtonCell(cell, appearance, states, zoomedSize, zoomFactor);
return cell;
}

// Stepper

static const std::array<IntSize, 4>& stepperSizes()
Expand Down Expand Up @@ -624,31 +488,25 @@ static LengthSize switchSize(const LengthSize& zoomedSize, float zoomFactor)
}
}

void ThemeMac::inflateControlPaintRect(StyleAppearance appearance, OptionSet<ControlStyle::State> states, FloatRect& zoomedRect, float zoomFactor) const
void ThemeMac::inflateControlPaintRect(StyleAppearance appearance, FloatRect& zoomedRect, float zoomFactor) const
{
BEGIN_BLOCK_OBJC_EXCEPTIONS
IntSize zoomRectSize = IntSize(zoomedRect.size());
switch (appearance) {
case StyleAppearance::Checkbox: {
// We inflate the rect as needed to account for padding included in the cell to accommodate the checkbox
// shadow" and the check. We don't consider this part of the bounds of the control in WebKit.
NSCell *cell = sharedCheckboxCell(states, zoomRectSize, zoomFactor);
NSControlSize controlSize = [cell controlSize];
IntSize zoomedSize = checkboxSizes()[controlSize];
auto size = controlSizeFromPixelSize(checkboxSizes(), zoomRectSize, zoomFactor);
auto zoomedSize = checkboxSizes()[size];
zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
zoomedSize.setWidth(zoomedSize.width() * zoomFactor);
zoomedRect = inflateRect(zoomedRect, zoomedSize, checkboxMargins(controlSize), zoomFactor);
zoomedRect = inflateRect(zoomedRect, zoomedSize, checkboxMargins(size), zoomFactor);
break;
}
case StyleAppearance::Radio: {
// We inflate the rect as needed to account for padding included in the cell to accommodate the radio button
// shadow". We don't consider this part of the bounds of the control in WebKit.
NSCell *cell = sharedRadioCell(states, zoomRectSize, zoomFactor);
NSControlSize controlSize = [cell controlSize];
IntSize zoomedSize = radioSizes()[controlSize];
auto size = controlSizeFromPixelSize(radioSizes(), zoomRectSize, zoomFactor);
auto zoomedSize = radioSizes()[size];
zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
zoomedSize.setWidth(zoomedSize.width() * zoomFactor);
zoomedRect = inflateRect(zoomedRect, zoomedSize, radioMargins(controlSize), zoomFactor);
zoomedRect = inflateRect(zoomedRect, zoomedSize, radioMargins(size), zoomFactor);
break;
}
case StyleAppearance::Switch: {
Expand All @@ -662,16 +520,14 @@ static LengthSize switchSize(const LengthSize& zoomedSize, float zoomFactor)
case StyleAppearance::PushButton:
case StyleAppearance::DefaultButton:
case StyleAppearance::Button: {
NSButtonCell *cell = button(appearance, states, zoomRectSize, zoomFactor);
NSControlSize controlSize = [cell controlSize];

// We inflate the rect as needed to account for the Aqua button's shadow.
if ([cell bezelStyle] == NSBezelStyleRounded) {
IntSize zoomedSize = buttonSizes()[controlSize];
zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
zoomedSize.setWidth(zoomedRect.width()); // Buttons don't ever constrain width, so the zoomed width can just be honored.
zoomedRect = inflateRect(zoomedRect, zoomedSize, buttonMargins(controlSize), zoomFactor);
}
auto largestControlSize = ThemeMac::supportsLargeFormControls() ? NSControlSizeLarge : NSControlSizeRegular;
if (zoomedRect.height() > buttonSizes()[largestControlSize].height() * zoomFactor)
break;
auto size = controlSizeFromPixelSize(buttonSizes(), zoomRectSize, zoomFactor);
auto zoomedSize = buttonSizes()[size];
zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
zoomedSize.setWidth(zoomedRect.width()); // Buttons don't ever constrain width, so the zoomed width can just be honored.
zoomedRect = inflateRect(zoomedRect, zoomedSize, buttonMargins(size), zoomFactor);
break;
}
case StyleAppearance::InnerSpinButton: {
Expand Down
10 changes: 2 additions & 8 deletions Source/WebCore/rendering/RenderThemeMac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -740,19 +740,13 @@ static FloatRect inflateRect(const FloatRect& rect, const IntSize& size, const i
switch (appearance) {
case StyleAppearance::Button:
case StyleAppearance::Checkbox:
#if ENABLE(INPUT_TYPE_COLOR)
case StyleAppearance::ColorWell:
#endif
case StyleAppearance::DefaultButton:
case StyleAppearance::InnerSpinButton:
case StyleAppearance::PushButton:
case StyleAppearance::Radio:
case StyleAppearance::SquareButton:
case StyleAppearance::Switch: {
auto states = extractControlStyleStatesForRenderer(renderer);
Theme::singleton().inflateControlPaintRect(renderer.style().effectiveAppearance(), states, rect, renderer.style().effectiveZoom());
case StyleAppearance::Switch:
Theme::singleton().inflateControlPaintRect(renderer.style().effectiveAppearance(), rect, renderer.style().effectiveZoom());
break;
}
case StyleAppearance::Menulist: {
auto zoomLevel = renderer.style().effectiveZoom();
setPopupButtonCellState(renderer, IntSize(rect.size()));
Expand Down

0 comments on commit 20d6a49

Please sign in to comment.