Skip to content

Commit ef75441

Browse files
author
Mike Renoir
committed
code syntax
1 parent 34ce4b3 commit ef75441

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1179
-157
lines changed

‎.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ url=git@github.com:overtake/rlottie.git
1616
[submodule "submodules/tg_owt"]
1717
path = submodules/tg_owt
1818
url = git@github.com:desktop-app/tg_owt.git
19+
[submodule "submodules/libprisma"]
20+
path = submodules/libprisma
21+
url = git@github.com:TelegramMessenger/libprisma.git
22+
[submodule "submodules/regex"]
23+
path = submodules/regex
24+
url = git@github.com:boostorg/regex.git

‎Telegram-Mac.xcworkspace/contents.xcworkspacedata

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Telegram-Mac/AppDelegate.swift

+29-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,40 @@ import ThemeSettings
2424
import ColorPalette
2525
import WebKit
2626
import System
27+
import CodeSyntax
28+
2729

2830
#if !APP_STORE
2931
import AppCenter
3032
import AppCenterCrashes
3133
#endif
3234

33-
35+
final class CodeSyntex {
36+
private let syntaxer: Syntaxer
37+
private init() {
38+
let pathFile = Bundle.main.path(forResource: "grammars", ofType: "dat")!
39+
let data = try! Data(contentsOf: URL(fileURLWithPath: pathFile))
40+
self.syntaxer = Syntaxer(data)!
41+
}
42+
private static let standart: CodeSyntex = .init()
43+
44+
fileprivate static func initialize() {
45+
_ = CodeSyntex.standart
46+
}
47+
48+
static func syntax(code: String, language: String, theme: SyntaxterTheme) -> NSAttributedString {
49+
return standart.syntaxer.syntax(code, language: language, theme: theme)
50+
}
51+
static func apply(_ code: NSAttributedString, to: NSMutableAttributedString, offset: Int) {
52+
code.enumerateAttributes(in: code.range, using: { value, innerRange, _ in
53+
if let font = value[.foregroundColor] as? NSColor {
54+
to.addAttribute(.foregroundColor, value: font, range: NSMakeRange(offset + innerRange.location, innerRange.length))
55+
} else if let font = value[.font] as? NSFont {
56+
to.addAttribute(.font, value: font, range: NSMakeRange(offset + innerRange.location, innerRange.length))
57+
}
58+
})
59+
}
60+
}
3461

3562
let enableBetaFeatures = true
3663

@@ -191,6 +218,7 @@ class AppDelegate: NSResponder, NSApplicationDelegate, NSUserNotificationCenterD
191218
private(set) var appEncryption: AppEncryptionParameters!
192219

193220
func applicationWillFinishLaunching(_ notification: Notification) {
221+
CodeSyntex.initialize()
194222
// UserDefaults.standard.set(true, forKey: "NSTableViewCanEstimateRowHeights")
195223
// UserDefaults.standard.removeObject(forKey: "NSTableViewCanEstimateRowHeights")
196224
}

‎Telegram-Mac/Appearance.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import ColorPalette
1919
import ThemeSettings
2020
#if !SHARE
2121
import InputView
22+
import CodeSyntax
2223
#endif
2324

2425
let premiumGradient = [NSColor(rgb: 0x6B93FF), NSColor(rgb: 0x976FFF), NSColor(rgb: 0xE46ACE)]
@@ -1625,7 +1626,7 @@ private func builtinBackgound(_ palette: ColorPalette) -> NSImage {
16251626
class TelegramPresentationTheme : PresentationTheme {
16261627
let chatList:TelegramChatListTheme
16271628
#if !SHARE
1628-
let chat: TelegramChatColors
1629+
let chat: TelegramChatColors
16291630
#endif
16301631
let cloudTheme: TelegramTheme?
16311632
let tabBar:TelegramTabBarTheme

‎Telegram-Mac/ApplicationContext.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import TelegramCore
77
import Localization
88
import InAppSettings
99
import IOKit
10+
import CodeSyntax
1011

1112
private final class AuthModalController : ModalController {
1213
override var background: NSColor {
@@ -515,11 +516,12 @@ final class AuthorizedApplicationContext: NSObject, SplitViewDelegate {
515516
self?.switchAccount(9, true)
516517
return .invoked
517518
}, with: self, for: .Nine, priority: .low, modifierFlags: [.control])
518-
519+
520+
519521

520522
#if DEBUG
521523
self.context.window.set(handler: { [weak self] _ -> KeyHandlerResult in
522-
context.bindings.rootNavigation().push(ExperimentalTextController(context))
524+
523525
return .invoked
524526
}, with: self, for: .T, priority: .supreme, modifierFlags: [.command])
525527

‎Telegram-Mac/ChatAccessoryModel.swift

+19-19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ChatAccessoryView : Button {
3030

3131
private weak var model: ChatAccessoryModel?
3232

33+
private let backgroundView = SimpleLayer()
34+
3335
private var quoteView: ImageView?
3436

3537
private let borderLayer = DashLayer()
@@ -43,7 +45,7 @@ class ChatAccessoryView : Button {
4345
headerView.isSelectable = false
4446
addSubview(headerView)
4547

46-
48+
self.layer?.addSublayer(backgroundView)
4749
self.layer?.addSublayer(borderLayer)
4850

4951
}
@@ -69,13 +71,13 @@ class ChatAccessoryView : Button {
6971

7072

7173

72-
let x: CGFloat = model.leftInset + (model.isSideAccessory ? 10 : 0)
74+
let x: CGFloat = model.leftInset// + (model.isSideAccessory ? 10 : 0)
7375

74-
let headerRect = CGRect(origin: NSMakePoint(x + model.mediaInset, (model.isSideAccessory ? 5 : 0) + model.topOffset + 2), size: headerView.frame.size)
76+
let headerRect = CGRect(origin: NSMakePoint(x + model.mediaInset, model.topOffset + 2), size: headerView.frame.size)
7577
transition.updateFrame(view: headerView, frame: headerRect)
7678

7779
if let textView = textView {
78-
let textRect = CGRect(origin: NSMakePoint(x, headerRect.height + (model.isSideAccessory ? 5 : 0) + model.topOffset + 2), size: textView.frame.size)
80+
let textRect = CGRect(origin: NSMakePoint(x, headerRect.height + model.topOffset + 2), size: textView.frame.size)
7981
transition.updateFrame(view: textView, frame: textRect)
8082
if let view = shimmerEffect {
8183
let rect = CGRect(origin: textRect.origin, size: view.frame.size)
@@ -86,16 +88,16 @@ class ChatAccessoryView : Button {
8688
if let quoteView = quoteView {
8789
transition.updateFrame(view: quoteView, frame: NSMakeRect(size.width - quoteView.frame.width - 2, 2, quoteView.frame.width, quoteView.frame.height))
8890
}
91+
transition.updateFrame(layer: backgroundView, frame: size.bounds)
8992

9093
}
9194

9295
func updateModel(_ model: ChatAccessoryModel, animated: Bool) {
9396
self.model = model
94-
self.backgroundColor = model.presentation.background
9597

9698
var cornerRadius: CGFloat = 0
9799
if model.modelType == .modern {
98-
cornerRadius = 4
100+
cornerRadius = .cornerRadius
99101
} else {
100102
if model.isSideAccessory {
101103
cornerRadius = .cornerRadius
@@ -120,9 +122,9 @@ class ChatAccessoryView : Button {
120122
x = 0
121123
height = model.size.height
122124
case .classic:
123-
x = model.isSideAccessory ? 10 : 0
124-
y = model.isSideAccessory ? 5 : 0 + model.topOffset
125-
height = model.size.height - model.topOffset - (model.isSideAccessory ? 10 : 0)
125+
x = 0
126+
y = model.topOffset
127+
height = model.size.height - model.topOffset
126128
cornerRadius = width / 2
127129
}
128130

@@ -319,9 +321,14 @@ class ChatAccessoryView : Button {
319321
if let model = model {
320322
switch model.modelType {
321323
case .modern:
322-
self.backgroundColor = model.presentation.colors.main.withAlphaComponent(0.1)
324+
self.backgroundView.backgroundColor = model.presentation.colors.main.withAlphaComponent(0.1).cgColor
323325
case .classic:
326+
self.backgroundView.backgroundColor = model.presentation.background.cgColor
327+
}
328+
if model.isSideAccessory {
324329
self.backgroundColor = model.presentation.background
330+
} else {
331+
self.backgroundColor = .clear
325332
}
326333
}
327334
}
@@ -449,10 +456,6 @@ class ChatAccessoryModel: NSObject {
449456
}
450457

451458
var modelType: ModelType {
452-
if isSideAccessory {
453-
return .classic
454-
}
455-
456459
return .modern
457460
}
458461

@@ -593,18 +596,15 @@ class ChatAccessoryModel: NSObject {
593596
self.sizeToFit = sizeToFit
594597

595598
header?.measure(width: width - leftInset - rightInset - (quoteIcon != nil ? 12 : 0))
596-
message?.measure(width: width - leftInset - rightInset)
599+
message?.measure(width: width - leftInset - rightInset - 30)
597600

598601
if let header = header, let message = message {
599602
var model_w = max(header.layoutSize.width + mediaInset, message.layoutSize.width) + leftInset + rightInset
600-
if isSideAccessory {
601-
model_w += 20
602-
}
603603
if quoteIcon != nil {
604604
model_w += 12
605605
}
606606
let width = sizeToFit ? model_w : width
607-
let height = max(38, header.layoutSize.height + message.layoutSize.height + yInset * 2 + (isSideAccessory ? 10 : 0))
607+
let height = max(38, header.layoutSize.height + message.layoutSize.height + yInset * 2)
608608
self.size = NSMakeSize(width, height)
609609
self.size.height += topOffset
610610
} else {

‎Telegram-Mac/ChatController.swift

+16-9
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,11 @@ class ChatControllerView : View, ChatInputDelegate {
426426

427427
view.layer?.removeAnimation(forKey: "opacity")
428428
view._change(pos: value.point, animated: animated && view.superview == superview, duration: 0.2, timingFunction: .easeOut)
429-
430-
if view.superview != superview {
431-
superview.addSubview(view)
429+
430+
let isNew = view.superview != superview
431+
superview.addSubview(view)
432+
433+
if isNew {
432434
let moveAsNew = currentAnimationRows.first(where: {
433435
$0.index == value.items.first?.index
434436
})
@@ -591,12 +593,15 @@ class ChatControllerView : View, ChatInputDelegate {
591593

592594
let visibleRows = tableView.visibleRows(frame.height)
593595

594-
for i in visibleRows.lowerBound ..< visibleRows.upperBound {
595-
let item = tableView.item(at: i)
596-
if let view = item.view as? ChatRowView {
597-
view.updateBackground(animated: transition.isAnimated, item: view.item)
596+
DispatchQueue.main.async {
597+
for i in visibleRows.lowerBound ..< visibleRows.upperBound {
598+
let item = self.tableView.item(at: i)
599+
if let view = item.view as? ChatRowView {
600+
view.updateBackground(animated: transition.isAnimated, item: view.item)
601+
}
598602
}
599603
}
604+
600605

601606
if let themeSelectorView = self.themeSelectorView {
602607
transition.updateFrame(view: themeSelectorView, frame: NSMakeRect(0, frame.height - themeSelectorView.frame.height, frame.width, themeSelectorView.frame.height))
@@ -876,10 +881,10 @@ class ChatControllerView : View, ChatInputDelegate {
876881

877882

878883
override func updateLocalizationAndTheme(theme: PresentationTheme) {
879-
let chatTheme = self.chatTheme ?? theme as! TelegramPresentationTheme
880884

881-
super.updateLocalizationAndTheme(theme: chatTheme)
885+
super.updateLocalizationAndTheme(theme: theme)
882886

887+
let chatTheme = self.chatTheme ?? theme as! TelegramPresentationTheme
883888

884889
if chatTheme.shouldBlurService, !isLite(.blur) {
885890
progressView?.blurBackground = chatTheme.blurServiceColor
@@ -6130,6 +6135,8 @@ class ChatController: EditableViewController<ChatControllerView>, Notifable, Tab
61306135
genericView.tableView.merge(with: transition, appearAnimated: appearAnimated)
61316136
collectFloatingPhotos(animated: animated && transition.state.isNone, currentAnimationRows: currentAnimationRows)
61326137

6138+
6139+
61336140
self.genericView.tableView.notifyScrollHandlers()
61346141

61356142
genericView.chatTheme = processedView.theme

‎Telegram-Mac/ChatGroupedItem.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class ChatGroupedItem: ChatRowItem {
102102
}
103103
if hasEntities {
104104

105-
caption = ChatMessageItem.applyMessageEntities(with: attributes, for: text, message: message, context: context, fontSize: theme.fontSize, openInfo:chatInteraction.openInfo, botCommand:chatInteraction.sendPlainText, hashtag: context.bindings.globalSearch, applyProxy: chatInteraction.applyProxy, textColor: theme.chat.textColor(isIncoming, entry.renderType == .bubble), linkColor: theme.chat.linkColor(isIncoming, entry.renderType == .bubble), monospacedPre: theme.chat.monospacedPreColor(isIncoming, entry.renderType == .bubble), monospacedCode: theme.chat.monospacedCodeColor(isIncoming, entry.renderType == .bubble), openBank: chatInteraction.openBank, blockColor: theme.chat.blockColor(context.peerNameColors, message: message, isIncoming: message.isIncoming(context.account, entry.renderType == .bubble), bubbled: entry.renderType == .bubble)).mutableCopy() as! NSMutableAttributedString
105+
caption = ChatMessageItem.applyMessageEntities(with: attributes, for: text, message: message, context: context, fontSize: theme.fontSize, openInfo:chatInteraction.openInfo, botCommand:chatInteraction.sendPlainText, hashtag: context.bindings.globalSearch, applyProxy: chatInteraction.applyProxy, textColor: theme.chat.textColor(isIncoming, entry.renderType == .bubble), linkColor: theme.chat.linkColor(isIncoming, entry.renderType == .bubble), monospacedPre: theme.chat.monospacedPreColor(isIncoming, entry.renderType == .bubble), monospacedCode: theme.chat.monospacedCodeColor(isIncoming, entry.renderType == .bubble), openBank: chatInteraction.openBank, blockColor: theme.chat.blockColor(context.peerNameColors, message: message, isIncoming: message.isIncoming(context.account, entry.renderType == .bubble), bubbled: entry.renderType == .bubble), isDark: theme.colors.isDark).mutableCopy() as! NSMutableAttributedString
106106
}
107107

108108
if !hasEntities || message.flags.contains(.Failed) || message.flags.contains(.Unsent) || message.flags.contains(.Sending) {

‎Telegram-Mac/ChatInputView.swift

+17-4
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,17 @@ class ChatInputView: View, Notifable {
553553

554554
self.textView.interactions.inputIsEnabled = self.isEnabled()
555555
self.textView.set(input)
556-
textView.placeholder = textPlaceholder
556+
self.textView.placeholder = textPlaceholder
557+
558+
if prevState.effectiveInput.inputText.isEmpty {
559+
self.textView.scrollToCursor()
560+
}
561+
562+
if state.effectiveInput != prevState.effectiveInput {
563+
if state.effectiveInput.inputText.count != prevState.effectiveInput.inputText.count {
564+
self.textView.scrollToCursor()
565+
}
566+
}
557567

558568
}
559569
private var updateFirstTime: Bool = true
@@ -827,6 +837,12 @@ class ChatInputView: View, Notifable {
827837

828838
let interaction = self.chatInteraction
829839

840+
defer {
841+
DispatchQueue.main.async { [weak self] in
842+
self?.textView.scrollToCursor()
843+
}
844+
}
845+
830846
if let window = kitWindow, self.chatState == .normal || self.chatState == .editing {
831847

832848
if let string = pasteboard.string(forType: .string) {
@@ -875,9 +891,6 @@ class ChatInputView: View, Notifable {
875891
return !result
876892
}
877893

878-
DispatchQueue.main.async { [weak self] in
879-
self?.textView.scrollToCursor()
880-
}
881894
return self.chatState != .normal
882895
}
883896

0 commit comments

Comments
 (0)