@@ -779,7 +779,8 @@ calc_element <- function(element, theme, verbose = FALSE, skip_blank = FALSE,
779
779
if (! is.null(el_out )) {
780
780
class <- element_tree [[element ]]$ class
781
781
if (inherits(class , " S7_class" )) {
782
- if (! S7 :: S7_inherits(el_out , class )) {
782
+ old_s3_inherit <- inherits(el_out , class @ name )
783
+ if (! S7 :: S7_inherits(el_out , class ) && ! old_s3_inherit ) {
783
784
cli :: cli_abort(" Theme element {.var {element}} must have class {.cls {class@name}}." , call = call )
784
785
}
785
786
} else {
@@ -923,6 +924,22 @@ S7::method(merge_element, list(margin, S7::class_any)) <-
923
924
new
924
925
}
925
926
927
+ # For backward compatibility
928
+ # TODO: in subsequent release cycle, start deprecation
929
+ S7 :: method(merge_element , list (S7 :: new_S3_class(" element" ), S7 :: class_any )) <-
930
+ function (new , old , ... ) {
931
+ if (is.null(old ) || is_theme_element(old , " blank" )) {
932
+ return (new )
933
+ }
934
+ if (S7 :: S7_inherits(old )) {
935
+ old <- S7 :: props(old )
936
+ }
937
+ idx <- lengths(new ) == 0
938
+ idx <- names(idx [idx ])
939
+ new [idx ] <- old [idx ]
940
+ new
941
+ }
942
+
926
943
# ' Combine the properties of two elements
927
944
# '
928
945
# ' @param e1 An element object
@@ -965,6 +982,12 @@ combine_elements <- function(e1, e2) {
965
982
}
966
983
}
967
984
985
+ # Backward compatbility
986
+ # TODO: deprecate next release cycle
987
+ if (inherits(e1 , " element" ) && is_theme_element(e2 )) {
988
+ return (combine_s3_elements(e1 , e2 ))
989
+ }
990
+
968
991
# If neither of e1 or e2 are element_* objects, return e1
969
992
if (! is_theme_element(e1 ) && ! is_theme_element(e2 )) {
970
993
return (e1 )
@@ -1010,6 +1033,24 @@ combine_elements <- function(e1, e2) {
1010
1033
e1
1011
1034
}
1012
1035
1036
+ # For backward compatibility
1037
+ # TODO: in subsequent release cycle, start deprecation
1038
+ combine_s3_elements <- function (e1 , e2 ) {
1039
+ if (S7 :: S7_inherits(e2 )) {
1040
+ e2 <- S7 :: props(e2 )
1041
+ }
1042
+ if (is_rel(e1 $ size )) {
1043
+ e1 $ size <- e2 $ size * unclass(e1 $ size )
1044
+ }
1045
+ if (is_rel(e1 $ linewidth )) {
1046
+ e1 $ linewidth <- e2 $ linewidth * unclass(e1 $ linewidth )
1047
+ }
1048
+ if (inherits(e1 , " element_text" )) {
1049
+ e1 $ margin <- combine_elements(e1 $ margin , e2 $ margin )
1050
+ }
1051
+ return (e1 )
1052
+ }
1053
+
1013
1054
# ' @export
1014
1055
`$.ggplot2::theme` <- function (x , ... ) {
1015
1056
.subset2(x , ... )
0 commit comments