Serenity Operating System
at portability 124 lines 3.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/SharedBuffer.h> 28#include <LibCore/ConfigFile.h> 29#include <LibGfx/SystemTheme.h> 30 31namespace Gfx { 32 33static SystemTheme dummy_theme; 34static const SystemTheme* theme_page = &dummy_theme; 35static RefPtr<SharedBuffer> theme_buffer; 36 37const SystemTheme& current_system_theme() 38{ 39 ASSERT(theme_page); 40 return *theme_page; 41} 42 43int current_system_theme_buffer_id() 44{ 45 ASSERT(theme_buffer); 46 return theme_buffer->shared_buffer_id(); 47} 48 49void set_system_theme(SharedBuffer& buffer) 50{ 51 theme_buffer = buffer; 52 theme_page = (SystemTheme*)theme_buffer->data(); 53} 54 55RefPtr<SharedBuffer> load_system_theme(const String& path) 56{ 57 auto file = Core::ConfigFile::open(path); 58 auto buffer = SharedBuffer::create_with_size(sizeof(SystemTheme)); 59 60 auto* data = (SystemTheme*)buffer->data(); 61 62 auto get_color = [&](auto& name) { 63 auto color_string = file->read_entry("Colors", name); 64 auto color = Color::from_string(color_string); 65 if (!color.has_value()) 66 return Color(Color::Black); 67 return color.value(); 68 }; 69 70#define DO_COLOR(x) \ 71 data->color[(int)ColorRole::x] = get_color(#x) 72 73 DO_COLOR(DesktopBackground); 74 DO_COLOR(ThreedHighlight); 75 DO_COLOR(ThreedShadow1); 76 DO_COLOR(ThreedShadow2); 77 DO_COLOR(HoverHighlight); 78 DO_COLOR(Selection); 79 DO_COLOR(SelectionText); 80 DO_COLOR(InactiveSelection); 81 DO_COLOR(InactiveSelectionText); 82 DO_COLOR(Window); 83 DO_COLOR(WindowText); 84 DO_COLOR(Base); 85 DO_COLOR(BaseText); 86 DO_COLOR(Button); 87 DO_COLOR(ButtonText); 88 DO_COLOR(DesktopBackground); 89 DO_COLOR(ActiveWindowBorder1); 90 DO_COLOR(ActiveWindowBorder2); 91 DO_COLOR(ActiveWindowTitle); 92 DO_COLOR(InactiveWindowBorder1); 93 DO_COLOR(InactiveWindowBorder2); 94 DO_COLOR(InactiveWindowTitle); 95 DO_COLOR(MovingWindowBorder1); 96 DO_COLOR(MovingWindowBorder2); 97 DO_COLOR(MovingWindowTitle); 98 DO_COLOR(HighlightWindowBorder1); 99 DO_COLOR(HighlightWindowBorder2); 100 DO_COLOR(HighlightWindowTitle); 101 DO_COLOR(MenuStripe); 102 DO_COLOR(MenuBase); 103 DO_COLOR(MenuBaseText); 104 DO_COLOR(MenuSelection); 105 DO_COLOR(MenuSelectionText); 106 DO_COLOR(RubberBandFill); 107 DO_COLOR(RubberBandBorder); 108 DO_COLOR(Link); 109 DO_COLOR(ActiveLink); 110 DO_COLOR(VisitedLink); 111 DO_COLOR(Ruler); 112 DO_COLOR(RulerBorder); 113 DO_COLOR(RulerActiveText); 114 DO_COLOR(RulerInactiveText); 115 DO_COLOR(TextCursor); 116 DO_COLOR(FocusOutline); 117 118 buffer->seal(); 119 buffer->share_globally(); 120 121 return buffer; 122} 123 124}