Skip to content

Commit d54e464

Browse files
committed
custom properties for theme elements
1 parent 8bd9f31 commit d54e464

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Collate:
176176
'grob-dotstack.R'
177177
'grob-null.R'
178178
'grouping.R'
179+
'properties.R'
179180
'theme-elements.R'
180181
'guide-.R'
181182
'guide-axis.R'

R/properties.R

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
property_boolean <- function(allow_null = FALSE, default = TRUE) {
3+
class <- S7::class_logical
4+
class <- if (allow_null) S7::new_union(class, NULL) else class
5+
validator <- function(value) {
6+
if ((allow_null && is.null(value)) || is_bool(value)) {
7+
return(character())
8+
}
9+
"must be a boolean"
10+
}
11+
S7::new_property(
12+
class = class,
13+
validator = validator,
14+
default = default
15+
)
16+
}
17+
18+
property_choice <- function(options, allow_null = FALSE, default = NULL) {
19+
force(options)
20+
class <- S7::class_character
21+
class <- if (allow_null) S7::new_union(class, NULL) else class
22+
validator <- function(value) {
23+
if (allow_null && is.null(value)) {
24+
return(character())
25+
}
26+
if (!is_string(value)) {
27+
return(as_cli("must be a string, not {.obj_type_friendly {value}}"))
28+
}
29+
if (value %in% options) {
30+
return(character())
31+
}
32+
as_cli("must be one of {.or {.val {options}}}")
33+
}
34+
S7::new_property(
35+
class = class,
36+
validator = validator,
37+
default = default
38+
)
39+
}
40+
41+
element_props <- list(
42+
fill = S7::new_property(
43+
S7::new_union(S7::class_character, S7::new_S3_class("GridPattern"), S7::class_logical, NULL),
44+
default = NULL
45+
),
46+
colour = S7::new_property(
47+
S7::new_union(S7::class_character, S7::class_logical, NULL),
48+
default = NULL
49+
),
50+
family = S7::new_property(
51+
S7::new_union(S7::class_character, NULL),
52+
default = NULL
53+
),
54+
hjust = S7::new_property(
55+
S7::new_union(S7::class_numeric, NULL),
56+
default = NULL
57+
),
58+
vjust = S7::new_property(
59+
S7::new_union(S7::class_numeric, NULL),
60+
default = NULL
61+
),
62+
angle = S7::new_property(
63+
S7::new_union(S7::class_numeric, NULL),
64+
default = NULL
65+
),
66+
size = S7::new_property(
67+
S7::new_union(S7::class_numeric, NULL),
68+
default = NULL
69+
),
70+
lineheight = S7::new_property(
71+
S7::new_union(S7::class_numeric, NULL),
72+
default = NULL
73+
),
74+
margin = S7::new_property(
75+
S7::new_union(S7::new_S3_class("margin"), NULL),
76+
default = NULL
77+
),
78+
face = property_choice(c("plain", "bold", "italic", "oblique", "bold.italic"), allow_null = TRUE),
79+
linewidth = S7::new_property(
80+
S7::new_union(S7::class_numeric, NULL),
81+
default = NULL
82+
),
83+
linetype = S7::new_property(
84+
S7::new_union(S7::class_numeric, S7::class_character, NULL),
85+
default = NULL
86+
),
87+
lineend = property_choice(c("round", "butt", "square"), allow_null = TRUE),
88+
shape = S7::new_property(
89+
S7::new_union(S7::class_numeric, S7::class_character, NULL),
90+
default = NULL
91+
),
92+
arrow = S7::new_property(
93+
S7::new_union(S7::new_S3_class("arrow"), S7::class_logical, NULL),
94+
default = NULL
95+
),
96+
arrow.fill = S7::new_property(
97+
S7::new_union(S7::class_character, S7::class_logical, NULL),
98+
default = NULL
99+
),
100+
debug = property_boolean(allow_null = TRUE, default = NULL),
101+
inherit.blank = property_boolean(default = FALSE)
102+
)

0 commit comments

Comments
 (0)