mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-02 15:46:33 +00:00
UI/AppKit: Fix tab title icon and text layout
Make the tab title icon and text vertical center
This commit is contained in:
parent
81596b4145
commit
e8ff9b6eb4
Notes:
github-actions[bot]
2024-10-15 15:45:22 +00:00
Author: https://github.com/bestwnh 🔰
Commit: e8ff9b6eb4
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1811
Reviewed-by: https://github.com/trflynn89 ✅
1 changed files with 12 additions and 10 deletions
|
@ -195,6 +195,11 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
|
||||||
|
|
||||||
- (void)updateTabTitleAndFavicon
|
- (void)updateTabTitleAndFavicon
|
||||||
{
|
{
|
||||||
|
static constexpr CGFloat TITLE_FONT_SIZE = 12;
|
||||||
|
static constexpr CGFloat FAVICON_SIZE = 16;
|
||||||
|
|
||||||
|
NSFont* title_font = [NSFont systemFontOfSize:TITLE_FONT_SIZE];
|
||||||
|
|
||||||
auto* favicon_attachment = [[NSTextAttachment alloc] init];
|
auto* favicon_attachment = [[NSTextAttachment alloc] init];
|
||||||
favicon_attachment.image = self.favicon;
|
favicon_attachment.image = self.favicon;
|
||||||
|
|
||||||
|
@ -206,20 +211,20 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
|
||||||
value:[NSColor clearColor]
|
value:[NSColor clearColor]
|
||||||
range:NSMakeRange(0, [favicon_attribute length])];
|
range:NSMakeRange(0, [favicon_attribute length])];
|
||||||
|
|
||||||
// By default, the text attachment will be aligned to the bottom of the string. We have to manually
|
// adjust the favicon image to middle center the title text
|
||||||
// try to center it vertically.
|
CGFloat offset_y = (title_font.capHeight - FAVICON_SIZE) / 2.f;
|
||||||
// FIXME: Figure out a way to programmatically arrive at a good NSBaselineOffsetAttributeName. Using
|
[favicon_attachment setBounds:CGRectMake(0, offset_y, FAVICON_SIZE, FAVICON_SIZE)];
|
||||||
// half the distance between the font's line height and the height of the favicon produces a
|
|
||||||
// value that results in the title being aligned too low still.
|
|
||||||
auto* title_attributes = @{
|
auto* title_attributes = @{
|
||||||
NSForegroundColorAttributeName : [NSColor textColor],
|
NSForegroundColorAttributeName : [NSColor textColor],
|
||||||
NSBaselineOffsetAttributeName : @3
|
NSFontAttributeName : title_font
|
||||||
};
|
};
|
||||||
|
|
||||||
auto* title_attribute = [[NSAttributedString alloc] initWithString:self.title
|
auto* title_attribute = [[NSAttributedString alloc] initWithString:self.title
|
||||||
attributes:title_attributes];
|
attributes:title_attributes];
|
||||||
|
|
||||||
auto* spacing_attribute = [[NSAttributedString alloc] initWithString:@" "];
|
auto* spacing_attribute = [[NSAttributedString alloc] initWithString:@" "
|
||||||
|
attributes:title_attributes];
|
||||||
|
|
||||||
auto* title_and_favicon = [[NSMutableAttributedString alloc] init];
|
auto* title_and_favicon = [[NSMutableAttributedString alloc] init];
|
||||||
[title_and_favicon appendAttributedString:favicon_attribute];
|
[title_and_favicon appendAttributedString:favicon_attribute];
|
||||||
|
@ -373,8 +378,6 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
|
||||||
|
|
||||||
- (void)onFaviconChange:(Gfx::Bitmap const&)bitmap
|
- (void)onFaviconChange:(Gfx::Bitmap const&)bitmap
|
||||||
{
|
{
|
||||||
static constexpr size_t FAVICON_SIZE = 16;
|
|
||||||
|
|
||||||
auto png = Gfx::PNGWriter::encode(bitmap);
|
auto png = Gfx::PNGWriter::encode(bitmap);
|
||||||
if (png.is_error()) {
|
if (png.is_error()) {
|
||||||
return;
|
return;
|
||||||
|
@ -385,7 +388,6 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
|
||||||
|
|
||||||
auto* favicon = [[NSImage alloc] initWithData:data];
|
auto* favicon = [[NSImage alloc] initWithData:data];
|
||||||
[favicon setResizingMode:NSImageResizingModeStretch];
|
[favicon setResizingMode:NSImageResizingModeStretch];
|
||||||
[favicon setSize:NSMakeSize(FAVICON_SIZE, FAVICON_SIZE)];
|
|
||||||
|
|
||||||
self.favicon = favicon;
|
self.favicon = favicon;
|
||||||
[self updateTabTitleAndFavicon];
|
[self updateTabTitleAndFavicon];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue